如果文件夹存在,mod_rewrite 将创建 301 而不是透明重定向

发布于 2024-09-13 22:28:39 字数 589 浏览 4 评论 0原文

我这里有一个小问题。 我使用一个简单的规则将所有请求重定向到脚本,除了一些具有静态内容的文件夹:

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 技术交流群。

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

发布评论

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

评论(1

墟烟 2024-09-20 22:28:39

这是由于 DirectorySlash 指令,它将在执行重写后执行外部重定向,这会产生意外的结果,即会带走添加的查询字符串。

您可以关闭DirectorySlash,但由于文档中描述的原因,不建议这样做。在这种情况下,首选的选项可能是只执行 mod_dir 的工作,在执行规则之前重定向到适当的以斜杠结尾的 URL。您现有规则之上的类似内容应该有效:

RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^.*$ /$0/ [R=301,L]

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 perform mod_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:

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