WordPress 自定义查询字符串和漂亮的 URL - 如何实现?
我有一个非常好的(到目前为止)新网站的 WordPress 设置。漂亮的网址按预期工作。
我有 1 个动态页面,根据查询字符串加载内容:
/dynamic/?loc=england&code=uk
我也希望使此 URL 变得“漂亮”,但每次修改 .htaccess 时都不会进行任何更改。尝试了所有方法并用谷歌搜索了其他所有方法 - 最后的手段。如果我能让以下工作发挥作用,我会很高兴。
我需要使 URL 看起来像。
/dynamic/location/england/code/uk/
将其添加到任何 .htaccess 都会破坏整个网站。
RewriteRule /dynamic/(.*)/(.*)/(.*)/(.*)/$ /dynamic?$1=$2&$3=$4
我缺少什么。
预先感谢
N
I have a perfectly good (so far) setup of wordpress of for a new website. The pretty urls are working as expected.
I have 1 dynamic page that loads content depending on the querystring:
/dynamic/?loc=england&code=uk
I wish to make this URL "pretty" as well but every-time I modify the .htaccess no changes are made. Tried everything and googled everything else - last resort. If I could get the following to work then I would be happy.
I need to make the URL look like.
/dynamic/location/england/code/uk/
Adding this to any .htaccess breaks the whole website.
RewriteRule /dynamic/(.*)/(.*)/(.*)/(.*)/$ /dynamic?$1=$2&$3=$4
What am i missing.
Thanks in advance
N
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不要将重写规则添加到您的
.htaccess
文件中。 WordPress 会为您管理该文件,因此请尽可能尝试使用内置功能。WordPress 实际上有一个稍微先进的重写引擎,它是标准的 - 而且它就像平台的其他部分一样是可插拔的。
不过,诀窍是利用它。您需要注册您的正则表达式,以便 WordPress 知道要匹配什么类型的字符串(即
dynamic/location/(.*)/code/(.*)
=>/dynamic? $loc=$1&code=$2
)。然后,您需要在后端设置页面和脚本来处理提交。对于类似的示例,请查看我收到的关于使用自定义帖子类型解析自定义 URL 的答案 WordPress 答案。扩展它,您需要设置类似于以下的代码(注意:未经测试!!!):
这应该将重写结构添加到您的
中。 htaccess 自动生成文件。
Don't add the rewrite rule to your
.htaccess
file. WordPress manages that file for you, so try to use built-in features whenever you can.WordPress actually has a somewhat advanced rewrite engine that ships standard - and it's pluggable just like the rest of the platform.
The trick, though, is working with it. You'll need to register your RegEx so WordPress knows what kinds of strings to match (i.e
dynamic/location/(.*)/code/(.*)
=>/dynamic?$loc=$1&code=$2
). Then you'll need to set up the page and script on the back end to handle the submission.For a similar example, look at the answer I received to parsing custom URLs with a custom post type over on WordPress Answers. Extending this, you'll need to set up code similar to the following (note: untested!!!):
This should add the rewrite structure to your
.htaccess
file automatically.