htaccess ReWriteCond ReWriteRule - 即使 URL 包含无意义字符也会发生重定向
我们刚刚完成了网站的重大重组,我正在尝试编写一组不同特异性的重定向规则。重定向只发挥了一半作用:
- 它们正确地重新路由旧 URL。
- 它们也错误地允许并重新路由包含
ReWriteCond 语句中未指定的文本的 URL(相反,我会预计会在浏览器中显示“未找到”错误消息。).
htaccess 文件(位于网站根目录中)中的语句包括:
RewriteBase /
RewriteCond %{REQUEST_URI} /company/company-history.html
RewriteRule (.*)$ http://www.technofrolics.com/about/index.html
RewriteCond %{REQUEST_URI} /press
RewriteRule (.*)$ http://www.technofrolics.com/gallery/index.html
上面正确执行了所需的重定向 但当我在域名后输入以下内容时也有效:
/youcanenteranytext/hereatall/anditstillworks/press
换句话说,域名后面和条件字符串前面的任何文本似乎都被允许/忽略。任何有关如何限制条件或重写规则以防止这种情况的建议将不胜感激!
谢谢,玛格丽塔
We've just finished a major re-structuring our website and I'm trying to write a set of redirect rules of varying specificity. The redirects are half working:
- They correctly re-route old URLs
- They incorrectly also allow and re-route URLs that include text not specified in the
ReWriteCond statements (when instead I would expect to see a "Not Found" error message displayed in the browser.)
Statements in the .htaccess file (located in the root of the web site) include:
RewriteBase /
RewriteCond %{REQUEST_URI} /company/company-history.html
RewriteRule (.*)$ http://www.technofrolics.com/about/index.html
RewriteCond %{REQUEST_URI} /press
RewriteRule (.*)$ http://www.technofrolics.com/gallery/index.html
The above correctly executes the desired redirect
but also works when I enter the following after the domain name:
/youcanenteranytext/hereatall/anditstillworks/press
In other words, any text following the domain and preceding the conditional string seems to be allowed/ignored. Any advise on how to restrict the condition or rewrite rule to prevent this would be much appreciated!
Thanks, Margarita
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您尝试匹配 %{REQUEST_URI} 时,您需要在正则表达式中包含边界,
^
表示匹配的开始。将使
/garbage/stuff/comapny/company-history.html
的请求不匹配。同样:将使其对
/youcanenteranytext/hereatall/anditstillworks/press
的请求不匹配。您还可以在正则表达式中使用$
来指示匹配的结束,例如:Will ONLY 匹配
/press
而不是/something/press
或/press/somethingelse
或/press/
。You need to including bounds in your regular expressions when you try to match against %{REQUEST_URI}, the
^
indicates the beginning of the match.Will make it so requests for
/garbage/stuff/comapny/company-history.html
won't match. And likewise:Will make it so requests for
/youcanenteranytext/hereatall/anditstillworks/press
won't match. You can additionally employ the$
in your regular expression to indicate the end of the match, so something like this:Will ONLY match requests for
/press
and not/something/press
or/press/somethingelse
or/press/
.