why does add_rewrite_rule refresh and loose url variables

I am using:

add_action( 'init', 'add_author_rules' );  
function add_author_rules() {   
    add_rewrite_rule(  
        "support/([^/]+)/?", "index.php?category=$matches[1]", "top");   
} 

in the themes/function.php

Read More

when I enter the address:

http://localhost/support/funding

the url changes to:

http://localhost

and the variable is not set:

print_r($_GET);
$category = get_query_var('category');
echo '!'.$category.'!';

both give an empty result

the .htaccess is unchanged and is:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

In case I wasnt clear I want:

/support/funding to remain in the address bar and me to have access to the variable category which would be ‘funding’ in this case

Related posts

Leave a Reply

1 comment

  1. First of all, always develop with debugging enabled so you can see errors. This will reveal the first issue: Undefined variable: matches. Your rule should be in single quotes so it’s passed as a string and the $matches variable is not expanded.

    The second issue is that there is no category query var, if you’re trying to set a default WordPress category, it should be category_name.

    This is tested and working in TwentyTwelve theme:

    add_action( 'init', 'add_author_rules' );  
    function add_author_rules() {   
        add_rewrite_rule(  
            'support/([^/]+)/?', 'index.php?category_name=$matches[1]', 'top'
        );   
    }
    

    Note that you will not see any change to .htaccess as this is an internal rewrite.

    If category is your own custom query var, you need to register query vars with WordPress so it is aware of them, though I would not suggest using just category.

    add_filter( 'query_vars', 'wpa_query_vars' );
    function wpa_query_vars($query_vars){
        $query_vars[] = 'wpa_category';
        return $query_vars;
    }