Support » Fixing WordPress » Using add_rewrite_rule causing redirect, losing the variable

  • I have a rewrite set up so that I can use a page structure like /stores/alabama/ and it will use the page /stores/ with a variable $state=alabama.

    I use the following code to achieve this:

    add_rewrite_rule('^stores/([^/]*)/?','index.php?post_type=page&name=stores&state=$matches[1]','top');
    function add_state_var($vars){
        $vars[] = 'state';
        return $vars;
    }
    add_filter( 'query_vars', 'add_state_var', 0, 1 );

    The problem is when you visit /stores/alabama/ it causes the page to redirect to /stores/ (Which is better than giving a 404 error which it did before, which means it’s not rewrite rules needing flushed).

    I removed the “post_type=page&name=stores” from the query variable and it in fact does not redirect, but instead goes to the landing page (however, it does have the query variable state=alabama).

    How can I make this URL point to a specific page and pass a query variable, without refreshing the page? Why is it refreshing the page?

Viewing 1 replies (of 1 total)
  • I just had the exact problem as you did. After a few hours of researching i found out that your permissions might affect the result of this function. Give your application writing permission to your .htaccess file, and you are good to go.

    Best Regards
    Sebastian

Viewing 1 replies (of 1 total)
  • The topic ‘Using add_rewrite_rule causing redirect, losing the variable’ is closed to new replies.