将 URL 重写规则添加到 Wordpress
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8476654/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add URL Rewrite Rule To Wordpress
提问by logic-unit
I've written a Wordpress plugin that adds a query string to the URL. However I can't seem to modify the htaccess to rewrite this. Not sure if Wordpress is overriding it?
我编写了一个 Wordpress 插件,可以向 URL 添加查询字符串。但是我似乎无法修改 htaccess 来重写它。不确定 Wordpress 是否覆盖它?
The current htaccess is:
当前的 htaccess 是:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
And the URL I'm trying to rewrite is:
我试图重写的 URL 是:
http://domain.com/deal-info/?id=87&post_name=testdealtitle
Desired URL:
所需网址:
http://domain.com/deal-info/87/testdealtitle
I've tried adding:
我试过添加:
RewriteRule ^([^/]*)/([^/]*)$ /deal-info/?id=&post_name= [L]
to no avail. Any ideas appreciated!
无济于事。任何想法表示赞赏!
采纳答案by logic-unit
After much confusion I found an answer here: https://wordpress.stackexchange.com/questions/5413/need-help-with-add-rewrite-rule
经过多次混淆后,我在这里找到了答案:https: //wordpress.stackexchange.com/questions/5413/need-help-with-add-rewrite-rule
Though I think the proper way to do it is by: http://codex.wordpress.org/Rewrite_API/add_rewrite_rule
虽然我认为正确的方法是通过:http: //codex.wordpress.org/Rewrite_API/add_rewrite_rule
I'm not sure if you can do this directly in htaccess, or I was having a conflict problem with another plugin I was using.
我不确定您是否可以直接在 htaccess 中执行此操作,或者我与正在使用的另一个插件存在冲突问题。
Thanks again to Olivier for his extensive answer!
再次感谢 Olivier 的广泛回答!
回答by Olivier Pons
When you do this:
当你这样做时:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
This means: "if it's not a file, and if it's not a dir, redirect allincoming URLs to
index.php
.
这意味着:“如果它不是文件,并且不是目录,则将所有传入的 URL重定向到
index.php
.
Your rewriterule should work... unless you've put it afterthe previous rewriterule. So, in short, are you sure your .htaccess it like that:
您的重写规则应该工作...除非你已经把它经过前面的重写规则。所以,简而言之,你确定你的 .htaccess 是这样的:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^([^/]*)/([^/]*)$ /deal-info/?id=&post_name= [L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Please tell me if it works.
请告诉我它是否有效。
Moreover if you only want to redirect onlydeal-info, you should do something like (not tested):
此外,如果你只想要重定向只处理-信息,你应该像做(未测试):
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^(/)?deal-info/([^/]*)/([^/]*)$ /deal-info/?id=&post_name= [L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Two hints:
两个提示:
Please try to use the RewriteLog
directive: it helps you to track down such problems:
请尝试使用该RewriteLog
指令:它可以帮助您追踪此类问题:
# Trace:
# (!) file gets big quickly, remove in prod environments:
RewriteLog "/web/logs/mywebsite.rewrite.log"
RewriteLogLevel 9
RewriteEngine On
My favorite tool to check for regexp:
我最喜欢的检查正则表达式的工具:
http://www.quanetic.com/Regex(don't forget to choose ereg(POSIX) instead of preg(PCRE)!)
http://www.quanetic.com/Regex(不要忘记选择 ereg(POSIX) 而不是 preg(PCRE)!)
May I ask you to add the rewritelog in your question?
我可以请您在问题中添加重写日志吗?