I have a wordpress page that takes a query parameter and customizes the page based on that (via javascript, the wordpress template is always the same). Its url looks like:
example.com/site/city-page?center=mycitycenter
I would like to rewrite it to:
example.com/site/city/mycitycenter
Where site
is a subdirectory where my wordpress instance is located.
I tried rewriting it as follows:
function add_city_query_var ( $query_vars ) {
$query_vars[] = 'center';
return $query_vars;
}
function add_rewrites() {
add_rewrite_rule(
'city/([^/]+)/?$',
'index.php?pagename=city-page¢er=$matches[1]',
'top'
);
}
add_filter('query_vars', 'add_city_query_var');
add_action( 'init', 'add_rewrites' );
I also tried:
add_action( 'generate_rewrite_rules', 'my_rewrite_rules' );
function my_rewrite_rules( $wp_rewrite )
{
$wp_rewrite->rules = array(
'city/([^/]+)/?$' => $wp_rewrite->index . '?pagename=city-page¢er=' . $wp_rewrite->preg_index( 1 )
) + $wp_rewrite->rules;
}
I made sure to go to permalinks page and save permalinks.
I tried via .htaccess:
RewriteRule ^city/([^/]*)$ /site/city-page/?center=$1 [L,QSA,NC]
with different placements of the above (above wordpress rewrites, below, within that block) and combinations of the flags.
In each case I am getting a wordpress 404 page.
I am able to access the target page fine and I can do a redirect (by adding R=301
flag in .htaccess
)
What am I missing here?
P.S. I tried the answers posted on URL rewrites and pagination and Rewrite rule not working and Wordpress Rewrite
city-page
, and I see that page when visitingcity/mycitycenter
. Note that you'll also have to update your javascript, since there is no longer a query string in the URL.