当尾部斜杠丢失时,.htaccess 不会被执行
这是我遇到过的最奇怪的问题。我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是
/name
是 apache 通常 设法映射到目录/name/
的文件名,但有时这种机制会失败。我遇到了一个非常相似的问题,并按照 URL 重写指南。我认为,在您的情况下,如果您有权访问它,您可以尝试将以下行(我建议在
RewriteEngine On
之后)添加到您的 Apache 配置文件中: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: