htaccess ReWriteCond ReWriteRule - 即使 URL 包含无意义字符也会发生重定向

发布于 2024-12-15 21:51:57 字数 707 浏览 0 评论 0原文

我们刚刚完成了网站的重大重组,我正在尝试编写一组不同特异性的重定向规则。重定向只发挥了一半作用:

  • 它们正确地重新路由旧 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜心 2024-12-22 21:51:57

当您尝试匹配 %{REQUEST_URI} 时,您需要在正则表达式中包含边界,^ 表示匹配的开始。

RewriteCond %{REQUEST_URI} ^/company/company-history\.html

将使 /garbage/stuff/comapny/company-history.html 的请求不匹配。同样:

RewriteCond %{REQUEST_URI} ^/press

将使其对 /youcanenteranytext/hereatall/anditstillworks/press 的请求不匹配。您还可以在正则表达式中使用 $ 来指示匹配的结束,例如:

RewriteCond %{REQUEST_URI} ^/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.

RewriteCond %{REQUEST_URI} ^/company/company-history\.html

Will make it so requests for /garbage/stuff/comapny/company-history.html won't match. And likewise:

RewriteCond %{REQUEST_URI} ^/press

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:

RewriteCond %{REQUEST_URI} ^/press$

Will ONLY match requests for /press and not /something/press or /press/somethingelse or /press/.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文