学课技术网
专注PHP学习资源

URL重写规则后WordPress页面查询字符串获取不到的问题解决方法

URL重写规则后WordPress页面查询字符串获取不到的问题解决方法 在2021-12-30 13:07 进行了更新,本站所有内容仅供大家仅供学习交流使用,请勿转载传播,严禁用于商业用途,请于24小时内删除,请支持正版!!!

URL重写规则后WordPress页面查询字符串获取不到的问题解决方法,这个是学课技术网主在开发过程中碰到的问题的解决办法,现在分享给大家。

问题描述:I have a WordPress site with a custom page comparison that gets a string.

URL重写规则后WordPress页面查询字符串获取不到的问题解决方法

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")

 收藏 (0) 打赏

您可以选择一种方式赞助本站

支付宝扫一扫赞助

微信钱包扫描赞助

未经允许不得转载,如果本站内容侵犯了您的个人利益,请联系QQ125252828告知。学课技术网 » URL重写规则后WordPress页面查询字符串获取不到的问题解决方法
分享到: 更多 (0)

相关推荐

  • 暂无文章

评论 抢沙发

评论前必须登录!

立即登录   注册

学课技术网专注软件汉化、分享、解密、绿化、Wordpress主题插件优化

联系我们技术支持
切换注册

登录

忘记密码 ?

您也可以使用第三方帐号快捷登录

切换登录

注册