add_rewrite_rule and pagination issue

I’ve got an issue, writing a correct rewrite rule.

Basically, here are the examples of the url I want :

/myPage/mySuPage

Then the same with pagination
/myPage/mySuPage/page/3

Then, the basic page, with 2 params, no pagination
/myPage/mySuPage/param1/param2

Then, the one with 2 parameters and the pagination
/myPage/mySuPage/param1/param2/page/3

More or less, i have it working with differents rules for each case, but if I put them in a row, nothing works.

Here are the rules I wrote :

add_rewrite_rule(
    'myPage/mySuPage/([^/]*)/([^/]*)/?',
    'index.php?pagename=myPage/mySuPage&param1=$matches[1]&param2=$matches[2]',
    'top' );

add_rewrite_rule(
    'myPage/mySuPage/([^/]*)/([^/]*)/page/([0-9]{1,})?',
    'index.php?pagename=myPage/mySuPage&param1=$matches[1]&param2=$matches[2]&pageds=$matches[3]',
    'top' );


add_rewrite_rule(
    'myPage/mySuPage/(.+?)(/page/([0-9]+))?/?$',
    'index.php?pagename=myPage/mySuPage&param1=$matches[1]&param2=$matches[2]',
    'top' );

1 Answer
1

Your first two rewrite rules do not end with $, which indicates that the URL should stop there. So myPage/mySuPage/param1/param2/page/3 would still be matched by the first pattern, because it can just ignored the /page/3 part at the end. The next rewrite rules will then never be used.

With my Rewrite analyzer plugin I was able to simplify your rewrite rules to these two: one without params and just optional paging, and one with params and optional paging. Combining these two into one did not work because the optional params woud “eat” the paging part.

add_rewrite_rule(
    'myPage/mySuPage(/page/([0-9]+)?)?/?$',
    'index.php?pagename=myPage/mySuPage&pageds=$matches[2]',
    'top'
);

add_rewrite_rule(
    'myPage/mySuPage/([^/]*)/([^/]*)(/page/([0-9]+)?)?/?$',
    'index.php?pagename=myPage/mySuPage&param1=$matches[1]&param2=$matches[2]&pageds=$matches[4]',
    'top'
);

Leave a Comment