问题描述:I have a WordPress site with a custom page comparison that gets a string.
Now it takes:
mysite.com/phones/compare/?between=samsung-galaxy-note-3-vs-nokia-lumia-730
It needs to be like that:
mysite.com/phones/compare/samsung-galaxy-note-3-vs-nokia-lumia-730
I’ve tried adding code on my site’s .htaccess that didn’t worked for me and went to WordPress 404 page:
RewriteRule ^phones/compare/(.+?)/?$ phones/compare/?between=$1 [NC,L]
Also I need to redirect to the new search engine friendly URL too when user gets to mysite.com/phones/compare/?between= URL.
解决办法:
You are getting a 404 page because WordPress internally doesn’t understand how to handle the URL. So the solution you are looking for will be a combination of .htaccess (for the redirection) plus a rewrite rule so that WordPress can handle the URL
Add the following to functions.php
add_action( 'init', 'init_custom_rewrite' );
function init_custom_rewrite() {
add_rewrite_rule(
'^phones/compare/([^/]*)/?',
'index.php?page_id=XXX&between=$matches[1]',
'top' );
}
Replace XXX
with the page_id of compare page, flush the rewrite rules by re-saving your permalink structure. Now add a rule to .htaccess for the redirection and that should work.
EDIT
Add the following code to functions.php to access between
query var in the page
add_filter('query_vars', 'my_query_vars', 10, 1);
function my_query_vars($vars) {
$vars[] = 'between';
return $vars;
}
And you can then access it using get_query_var("between")
本站资源仅供下载者学习技术,版权归资源原作者所有,请在下载后24小时之内自觉删除。
本站资源仅供下载者学习IT编程开发技术,请遵守国家法律法规,严禁用于非法用途。
若作商业用途,请购买正版,由于未及时购买正版发生的侵权行为,与本站无关。
如您是版权方,本站源码有侵犯到您的权益,请邮件联系331752841@qq.com 删除,我们将及时处理!