.htaccess 重写条件
此规则在 .htaccess 文件中工作得很好:
ErrorDocument 403 /AccessDenied.html
ErrorDocument 404 /NotFound.html
RewriteEngine on
RewriteBase /
RewriteRule ^(index(\.(html|htm))?)$ / [R]
作为条件编写的相同规则不起作用:
ErrorDocument 403 /AccessDenied.html
ErrorDocument 404 /NotFound.html
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} ^(index(\.(html|htm))?)$
RewriteRule ^(index(\.(html|htm))?)$ / [R]
我想我的问题是为什么该规则在第一个场景中有效,但在第二个场景中不起作用?我该如何解决它?
This rule works just fine in a .htaccess file:
ErrorDocument 403 /AccessDenied.html
ErrorDocument 404 /NotFound.html
RewriteEngine on
RewriteBase /
RewriteRule ^(index(\.(html|htm))?)$ / [R]
The same rule written as a conditional doesn't work:
ErrorDocument 403 /AccessDenied.html
ErrorDocument 404 /NotFound.html
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} ^(index(\.(html|htm))?)$
RewriteRule ^(index(\.(html|htm))?)$ / [R]
I suppose my question is that why is the rule working in the first scenario, but not in the second? And how can I fix it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信您的问题是
RewriteBase
处理(始终删除前导斜杠)不会影响%{REQUEST_URI}
等变量,并且不适用于无论如何,RewriteCond 都会进行处理。
因此,模式
^(index(\.(html|htm))?)$
在RewriteRule
中可以正常工作,但在RewriteCond
中则不行> 根据请求 URI 进行匹配。您需要包含前导斜杠(至少如果您使用开头^
锚点):I believe your problem is that the
RewriteBase
processing (which always strips the leading slash) doesn't affect variables like%{REQUEST_URI}
and doesn't apply forRewriteCond
processing anyway.So, the pattern
^(index(\.(html|htm))?)$
will work fine in aRewriteRule
, but not aRewriteCond
matching against the request URI. You need to include that leading slash (at least if you're using the beginning^
anchor):