最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

url rewriting - Having trouble with using add_rewrite_rule and pagination

matteradmin9PV0评论

We have a section on the site, such as:

www.example/for-sale/category-name/

for-sale is a WP page, however category-name is dynamic and is what I wrote the below rewrite rule for.

add_rewrite_rule('for-sale\/([a-z-]+)\/?$', 'index.php?pagename=for-sale&search_slug=$matches[1]', 'top');

However, problems arise when the user needs to use the page query var, if I try going to:

www.example/for-sale/category-name/?page=2

it get's rewritten to:

www.example/for-sale/2/

So completely strips out the category slug.

Is there a way I can get this to work?

...or preferably I would like to be able to use something like this:

www.example/for-sale/category-name/2/

So I tried adding this additional rewrite rule:

add_rewrite_rule('for-sale\/([a-z-]+)\/([0-9])+\/?$', 'index.php?pagename=for-sale&search_slug=$matches[1]&page=$matches[2]', 'top');

..but again, it redirected back to:

www.example/for-sale/2/

What am I missing here?

We have a section on the site, such as:

www.example/for-sale/category-name/

for-sale is a WP page, however category-name is dynamic and is what I wrote the below rewrite rule for.

add_rewrite_rule('for-sale\/([a-z-]+)\/?$', 'index.php?pagename=for-sale&search_slug=$matches[1]', 'top');

However, problems arise when the user needs to use the page query var, if I try going to:

www.example/for-sale/category-name/?page=2

it get's rewritten to:

www.example/for-sale/2/

So completely strips out the category slug.

Is there a way I can get this to work?

...or preferably I would like to be able to use something like this:

www.example/for-sale/category-name/2/

So I tried adding this additional rewrite rule:

add_rewrite_rule('for-sale\/([a-z-]+)\/([0-9])+\/?$', 'index.php?pagename=for-sale&search_slug=$matches[1]&page=$matches[2]', 'top');

..but again, it redirected back to:

www.example/for-sale/2/

What am I missing here?

Share Improve this question asked Mar 29, 2019 at 22:45 BrettBrett 4145 silver badges25 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 4

Your rewrite rules are good, but the redirect happens because WordPress applies canonical redirect via its redirect_canonical() function which is hooked to template_redirect.

And you can cancel the redirect via the parse_request action, like so, where we check if the matched rewrite rule is the one you set when you call add_rewrite_rule() and if it is, then cancel the redirect by "unhooking" redirect_canonical() from the template_redirect action:

add_action( 'parse_request', function( $wp ){
    if ( 'for-sale\/([a-z-]+)\/([0-9])+\/?$' === $wp->matched_rule ) {
        remove_action( 'template_redirect', 'redirect_canonical' );
    }
} );
Post a comment

comment list (0)

  1. No comments so far