如果文件夹存在,mod_rewrite 将创建 301 而不是透明重定向
我这里有一个小问题。 我使用一个简单的规则将所有请求重定向到脚本,除了一些具有静态内容的文件夹:
RewriteEngine On
RewriteCond $1 !^(templates|css|js|uploads)/(.*)$
RewriteRule ^(.*)$ index.php?url=$1 [L,QSA]
大多数时候,重定向是良好且透明的。 但是,如果与 URL 匹配的文件夹存在,并且如果我不输入最后一个“/”,我将得到 301 重定向。
示例:(文件夹库不存在,但 mods 存在)
- ht*p://localhost/test/gallery/ ->好的
- ht*p://localhost/test/mods/ ->好的
- htp://localhost/test/mods -> 301 重定向到 htp://localhost/test/mods/?url=mods
我在所有 apache2 服务器上都遇到这个问题(经过测试的 Fedora、Debian、Windows)。
有人知道如何解决这个问题吗? 谢谢
i'm having a small problem here.
I'm using a simple rule to redirect all requests to a script, excepts some folders with static content :
RewriteEngine On
RewriteCond $1 !^(templates|css|js|uploads)/(.*)$
RewriteRule ^(.*)$ index.php?url=$1 [L,QSA]
Most of the time, the redirection is good and transparent.
But if the folder matching the URL exists and if i don't put the last "/", i will have a 301 redirect.
Examples : (the folder gallery doesn't exists but mods does)
- ht*p://localhost/test/gallery/ -> OK
- ht*p://localhost/test/mods/ -> OK
- htp://localhost/test/mods -> 301 redirection to htp://localhost/test/mods/?url=mods
I have this problem on all apache2 servers (tested Fedora, Debian, Windows).
Someone knows how to solve this ?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是由于
DirectorySlash
指令,它将在执行重写后执行外部重定向,这会产生意外的结果,即会带走添加的查询字符串。您可以关闭
DirectorySlash
,但由于文档中描述的原因,不建议这样做。在这种情况下,首选的选项可能是只执行mod_dir
的工作,在执行规则之前重定向到适当的以斜杠结尾的 URL。您现有规则之上的类似内容应该有效:This is due to the
DirectorySlash
directive, which will perform the external redirection after your rewrite is performed, which has the unintended consequence of taking your added query string with it.You can turn
DirectorySlash
off, but it's not recommended for the reasons described in the documentation. The preferred option in this case is probably to just performmod_dir
's work for it, redirecting to the appropriate slash-terminated URL before performing your rules. Something like this above your existing rule should work: