1

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&center=$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&center=' . $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

  • Can you confirm that you are using Apache. login as sudo user via ssh and run apache -v then run nginx -v I want to be clear of what server you are running. Apache or Nginx. The reason I ask is that rewrites are achieved differently depending on what server you are running. – SEO DEVS Oct 6 '17 at 5:11
  • Yes, I am running Apache, I confirmed with the hosting provider (as I don't have shell access). Also, the rewrite rule is working as a redirect (via R=301 flag), and it's picking up other directives (like redirects) in the .htaccess file – Alex M Oct 6 '17 at 21:29
  • A copy/paste of your first example works for me with 2016 theme and no plugins. I added a page with slug city-page, and I see that page when visiting city/mycitycenter. Note that you'll also have to update your javascript, since there is no longer a query string in the URL. – Milo Oct 7 '17 at 0:23
  • Hmm, interesting. I'll try it on my local and see – Alex M Oct 7 '17 at 20:37
  • I had the same issue, solved it here: wordpress.stackexchange.com/questions/282253/… – lukgoh Oct 8 '17 at 22:42
0

Just use this rewrite rule in your htaccess file, it will strip all query strings.

RewriteRule ^(.*) /$1? [R=301,L]

to allow more rules to follow use this instead

RewriteRule ^(.*) /$1? [R=301]

remove 301 like this

RewriteRule ^(.*) /$1? [NE,L]

and refer the correct url for search engines.

without 301 you will need to make a canonical link pointing to the correct url.

if you are rewriting the URL for SEO then a better way might be to block search engines from crawling pages with querry strings by adding this to your robots txt file.

Disallow: /site/city-page/?*

This rule presumes you site directory is e.g /site/city-page/

Hope this helps

  • Sorry, I'm not understanding your suggestion. I don't want to strip out query parameters, I want to rewrite the URL to one that is more user friendly and looks nicer. What does the 301 redirect do to help here? The idea about adding canonical URLs is good, I will plan to implement that, but I think it's easier to do that via wordpress template – Alex M Oct 7 '17 at 20:34
  • Yes, the rule given will strip the query's from the URL so it makes nice urls, the 301 is used because the URL with the query param is no longer available. When the rewrite is used without 301 you would get a 404 error which is ok but if you already have page authority for your pages then you need a 301 so you keep your search rankings. 301 will say to google that mydomain.com/city-page?center=mycitycenter is still available but it has moved to mydomain.com/mycitycenter. With a 301 there will be no need for canonicals – SEO DEVS Oct 8 '17 at 8:43
  • This isn't what I'm trying to accomplish. I want the URLs rewritten, not redirected. This is for a new feature so there are no links to pages with the query parameter. I want to launch the pages with a nicer looking URL. – Alex M Oct 8 '17 at 18:32
  • I have updated the answer for you. try the second rewrite rule given in the answer this will allow the rewrite without redirecting all the sauce by removing the "L". also try with the third option which removes the 301 but I feel this may give a 404 error. its worth a shot. – SEO DEVS Oct 8 '17 at 22:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.