http RewriteCond 和 RewriteRule
我需要重写 URL,例如 http://www.example.com/search.php?abc 到http://www.example.com/abc 使用下面的规则,但它不起作用,为什么不匹配?
RewriteCond %{REQUEST_URI} ^search.php RewriteRule ^search.php?q=([-0-9a-zA-Z]+) $1
I need to rewrite URL like http://www.example.com/search.php?abc
to http://www.example.com/abc
using the rules below, but it doesn't work, why doesn't it match?
RewriteCond %{REQUEST_URI} ^search.php RewriteRule ^search.php?q=([-0-9a-zA-Z]+) $1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
%{REQUEST_URI}
始终以/
开头。因此RewriteCond %{REQUEST_URI} ^search.php
永远不会匹配。重写规则指令
<块引用>
Pattern 是 Perl 兼容的正则表达式。在第一个 RewriteRule 上,它应用于请求的(%-解码)URL 路径;后续模式将应用于最后匹配的 RewriteRule 的输出。
?
表示在未转义时匹配一个或不匹配前一个字符。所以你的^search.php?q=...
将匹配:search.phpq=...
search.phq=...
来自 PCRE 手册页
你必须这样做:
NC
(nocase):flag_nc Apache 文档L
(最后一个) : flag_l Apache 文档%{REQUEST_URI}
always has a/
at the start. soRewriteCond %{REQUEST_URI} ^search.php
will never match.RewriteRule Directive
?
means match for one or none of previous character when not escaped. So your^search.php?q=...
would match:search.phpq=...
search.phq=...
from PCRE man pages
You will have to do this:
NC
(nocase) : flag_nc Apache DocsL
(last) : flag_l Apache Docs