I'm trying to get WordPress to recognize a URL structure like this:


The basic ideea is that I need to access the id, first_name and last_name in my page and they have to come via the URL. So I have to put rules like:

(/some_page/child_page/)/([0-9]+)/([^/]+)/([^/]+) =>

for every page, by using the page_rewrite_rules filter.

The question is: How to I get the "pagename" for every page? The /some_page/child_page/ part. I can get the post_name but then I miss the parent, grandfather page.

1 Answer 1


The pattern WordPress uses to recognize pages is (.+?), which will match anything, but is not greedy, so it allows you to put something at the end. The following code works for me in WordPress 3.0.1: it places the extra pattern at the second-to-last place of the list, because the final pattern is so generic it will match every URL:

add_filter( 'page_rewrite_rules', 'wpse7243_page_rewrite_rules' );
function wpse7243_page_rewrite_rules( $rewrite_rules )
    // The most generic page rewrite rule is at end of the array
    // We place our rule one before that
    end( $rewrite_rules );
    $last_pattern = key( $rewrite_rules );
    $last_replacement = array_pop( $rewrite_rules );
    $rewrite_rules +=  array(
        '(.+?)/([0-9]+)/([^/]+)/([^/]+)/?$' => 'index.php?pagename=$matches[1]&id=$matches[2]&fname=$matches[3]&lname=$matches[4]',
        $last_pattern => $last_replacement,
    return $rewrite_rules;

Be aware that pages, like any post, can also be split into multiple pages with the <!--nextpage--> snippet, and the normal URL for that is /pagename/2/, which might be confusing when you also have /pagename/3/jan/fabry/ where the 3 is an ID. You can change the pattern to include an optional page number at the end, so this can become /pagename/3/jan/fabry/2/ where the final 2 is the page number, and the first 3 is the ID.

If you are changing the rewrite rules I recommend my rewrite analyzer plugin (soon in the repository, but get the current version via Dropbox), it helps you debug these things.

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.