当尾部斜杠丢失时,.htaccess 不会被执行

发布于 2024-11-24 01:37:48 字数 441 浏览 0 评论 0原文

这是我遇到过的最奇怪的问题。我在 Apache/2.2.13 (Linux/SUSE) 的 .htaccess 中使用 mod_rewrite,当 URL 中没有尾部斜杠时,它似乎不会被调用。我通过在 .htaccess 文件中放入垃圾进行了测试,当添加尾部斜杠时仅收到 500 个错误,而省略时则收到 404 个错误。老实说我不知道​​为什么。

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

/name fails with 404
/name/ is successful

This is the weirdest problem I have encountered. I am using mod_rewrite in .htaccess in Apache/2.2.13 (Linux/SUSE), and it appears to not be called when there is no trailing slash in the URL. I tested by putting garbage in the .htaccess file, and only received 500 errors when the trailing slash was added, but 404 when omitted. I honestly have no idea why.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

/name fails with 404
/name/ is successful

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

安静 2024-12-01 01:37:48

问题是 /name 是 apache 通常 设法映射到目录 /name/ 的文件名,但有时这种机制会失败。我遇到了一个非常相似的问题,并按照 URL 重写指南

我认为,在您的情况下,如果您有权访问它,您可以尝试将以下行(我建议在 RewriteEngine On 之后)添加到您的 Apache 配置文件中:

 RewriteBase    /
 RewriteRule    ^name$  name/  [R]

The issue is that /name is a file name that apache usually manages to map to the directory /name/, but sometimes this mechanism fails. I've had a remotely similar problem and followed the suggestions to solve the problem according to the Trailing Slash Problem section of the URL Rewriting Guide in Apache's documentation.

I'd think that in your case you could try to add the following lines (I'd suggest right after RewriteEngine On) to your Apache configuration file, if you have access to it:

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