I have a post type 'destinations' which should work without the custom post type slug, but it should be hierarchical. So a page like this can exist: /madagascar/grouptrip/information I wanted to creat a rewrite rule to match the main parents (madagascar and uganda) and match all child pages hierarchical. So I had a look at the rewrite rule used for pages:


and added my main parents in front of it:

add_rewrite_rule('(madagascar|uganda)/(.?.+?)?(:/([0-9]+))?/?$', 'index.php?destination=$matches[1]/$matches[2]&page=$matches[4]', 'top');

This is working for my example url /madagascar/grouptrip/information However if I add a pagenumber at the end /madagascar/grouptrip/information/2 it isn't working. The exact same structure is matched by the regex for pages which I copied. Only when i remove the colon from the regex the rewrite rule is working and I don't understand why?

So to sum it up:


is working for a structure like:




is not working for a structure like:




is working. Why isn't the copied regex working?

1 Answer 1


so the problem with the colon is that it has no special meaning so your regex is looking to match :/ literally - that would work for a string with a structure like:


you would get a full match and 4 groups:

    [0] => 'madagascar/subpage:/2'
    [1] => 'madagascar'
    [2] => 'subpage'
    [3] => ':/2'
    [4] => '2'

but since the colon is not in your tested string, all you get is a full match and two groups (only parts (madagascar|uganda)and (.?.+?)? tested positive)

    [0] => 'madagascar/subpage/2'
    [1] => 'madagascar'
    [2] => 'subpage/2'
  • Thank you for your answer, however this does not explain why the same regex (.?.+?)?(:/([0-9]+))?/?$ is working for this structure: /pagename/subpage/2
    – Rockfire
    Commented Apr 3, 2018 at 9:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.