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

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

常见问题
Wordpress模板插件上传安装提示错误怎么办?
本站在打包插件或者模板的时候,一般会将官方文档、官方申明、已经其他文件一起打包,因此,大部分插件模板在安装前需要解压缩。
购买后无法下载或者提取码错误怎么办?
请联系客服QQ:125252828 或者微信:dobunkan,由于骚扰信息过多,加好友时请说明原由!
小白建站啥都不会怎么办?
请与客服联系,沟通具体的技术支持。由于搭建环境、调试程序需要花费一些时间,可能会收取一些费用。
怎样催促站长更新?
直接微信或者QQ联系本站客服,提供最新版本号以及软件名称,客服会在第一时间更新,如果超过2-3年未更新的请先联系站长后再确认是否需要下载。
为什么打开链接看不到最新版本?
因为网盘默认是按名称排序,最新版本不一定会排在第一个,请打开链接后点击右上角的“修改日期”,这时候更新列表就会按更新日期排序了。
原文链接:https://www.dobunkan.com/tools/27048,转载请注明出处。
0

评论0

请先
显示验证码
没有账号?注册  忘记密码?

社交账号快速登录

学课技术网欢迎您的加入!