admin管理员组

文章数量:1122846

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/site/city-page?center=mycitycenter

I would like to rewrite it to:

example/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

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

Share Improve this question asked Oct 5, 2017 at 20:14 Alex MAlex M 111 bronze badge 5
  • 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 Commented Oct 6, 2017 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 Commented Oct 6, 2017 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 Commented Oct 7, 2017 at 0:23
  • Hmm, interesting. I'll try it on my local and see – Alex M Commented Oct 7, 2017 at 20:37
  • I had the same issue, solved it here: wordpress.stackexchange.com/questions/282253/… – lukgoh Commented Oct 8, 2017 at 22:42
Add a comment  | 

1 Answer 1

Reset to default 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

本文标签: URL rewriting not working