setting pagename with rewrite rule

I’m wanting to set the pagename dynamically in a rewrite rule.

I have a theme with a page-template that uses a rewrite rule, but in order for it to work, the pagename has to be set to something specific.


So, in my code, the pagename is set to props. I would like that portion to be set by a $matches[] so that any url that is a page and is trailed by the /s/ matches the rule and sets the mls variable.

  • Patrick
    • Support Monkey

    Hi @tim

    LOL. You had asked this in live support yesterday, and I couldn’t answer you there. Darned if I don’t catch your same question during my designated hours in the forums! :slight_smile:

    I’ll see if I can find a support staff member with more expertise than I in this matter. Hang on…

  • Tim
    • WPMU DEV Initiate

    I’ve tried doing things like:

    add_rewrite_rule('([^/]*)/s/([^/]*)/?','index.php?pagename=$matches[0]&alt=true&mls=$matches[1]','top'); – This doesn’t work.

    add_rewrite_rule(^prop/s/([^/]*)/?','index.php?pagename=$matches[0]&alt=true&mls=$matches[1]','top'); – This catches the pagename in the matches, but it still doesn’t allow for a dynamic pagename.

  • Arnold
    • El Macho WP

    You’d normally do that with an added endpoint.

    In init

    add_rewrite_endpoint('s', EP_PAGES);

    Then in ‘request’ hook.

    function on_request($vars){
    //Make sure something is actually set
    if(isset($vars['s']) ) $vars['s'] = true;
    return $vars;

    Then you can test query vars anywhere for the “s” variable.

    One problem with your scheme. ‘s’ is used as the default search variable so it’s reserved I’m pretty sure.

    Need a different letter or string that isn’t reserved. there is a long list

  • Tim
    • WPMU DEV Initiate

    I don’t understand how this applies to the rewrite rules. If I’m to understand the wordpress docs, the add_rewrite_endpoint adds something like /categories/. If the endpoint is named /prop/ :

    add_rewrite_endpoint('prop', EP_PAGES);

    Where do you put this? In the functions.php file? The mu-plugins folder?

    Is accessing the data after that endpoint the same as a rewrite rule?


  • Arnold
    • El Macho WP

    I thought “prop” was your page slug? Iused “s” above which can add an end point for the props page. /props/s/ which is what you asked for. I just warned you that “s” is a reserved query var so you can’t use it.

    Something like /props/find/ might work

    The article you reference is a good one. It may be the one where I learned how to do it originally.

    I mainly use it for “edit” endpoint so I have urls like

    I add the /edit/

    and add code that redirects toa front end edit page for that listing. Similar thing with /contact/

    takes them to an email contact form already set for the author of the post.

  • Tim
    • WPMU DEV Initiate

    Correct, /prop/ was originally the slug, but the overall idea was to have a rewrite rule independent of pagename/slug. In my example/follow up question, I had changed it to be the endpoint.

    In my final iteration, the code is:

    add_action('init', 'mls_add_endpoints');

    function mls_add_endpoints()
    add_rewrite_endpoint('mls', EP_PAGES);


    From there I can assign page-templates and do what I want:

    if(isset($wp_query->query_vars['mls'])) {
    $mls = urldecode($wp_query->query_vars['mls']);
    //Do other stuff

    Thanks for you help. Ultimately, it falls to WordPress docs not being clear on how the function works. Had the endpoint docs said from the beginning that whatever follows it sets that query variable value, it would have saved a HUGE amount of headache.

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.