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¢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
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
Share Improve this question asked Oct 5, 2017 at 20:14 Alex MAlex M 111 bronze badge 5 |1 Answer
Reset to default 0Just 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
版权声明:本文标题:URL rewriting not working 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736304340a1932200.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
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. – Milo Commented Oct 7, 2017 at 0:23