.htaccess 在省略尾部斜杠时更改 URL
我在一个根域中有多个带有 .htaccess
的站点。 当我去 http://sub.site1.org/files/
时
RewriteEngine On
RewriteCond %{HTTP_HOST} ^192\.168\.0\.6$
RewriteCond %{REQUEST_URI} !/192.168.0.6/
RewriteRule ^(.*)$ /192.168.0.6/$1 [L]
RewriteCond %{HTTP_HOST} ^sub\.site1\.org$
RewriteCond %{REQUEST_URI} !/192.168.0.6/
RewriteRule ^(.*)$ /192.168.0.6/$1 [L]
RewriteCond %{HTTP_HOST} ^site2\.com$
RewriteCond %{REQUEST_URI} !/site2.com/
RewriteRule ^(.*)$ /site2.com/$1 [L]
它可以工作,但是当我去 http://sub.site1.org/files
时code> (不带斜杠),它将我重定向到 http://sub.site1.org/192.168.0.6/files
。
有办法解决吗?
I have a multiple sites in one root domain with .htaccess
. There is my
RewriteEngine On
RewriteCond %{HTTP_HOST} ^192\.168\.0\.6$
RewriteCond %{REQUEST_URI} !/192.168.0.6/
RewriteRule ^(.*)$ /192.168.0.6/$1 [L]
RewriteCond %{HTTP_HOST} ^sub\.site1\.org$
RewriteCond %{REQUEST_URI} !/192.168.0.6/
RewriteRule ^(.*)$ /192.168.0.6/$1 [L]
RewriteCond %{HTTP_HOST} ^site2\.com$
RewriteCond %{REQUEST_URI} !/site2.com/
RewriteRule ^(.*)$ /site2.com/$1 [L]
When I go to http://sub.site1.org/files/
it works, but when I go to http://sub.site1.org/files
(without a slash), it redirects me to http://sub.site1.org/192.168.0.6/files
.
Any way to fix that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您似乎与 mod_dir 有冲突。如果
/files
作为/192.168.0.6/files
中的物理目录存在,则 mod_dir 将在尾部斜杠处附加 301 重定向(以便“修复”URL) 在 URL 被重写之后,从而暴露了重写的 URL。 (您应该在重定向的 URL 上看到一个尾部斜杠。)您需要在重写请求之前手动附加尾部斜杠,方法是检查 URL 是否映射到新位置的目录。
如果
/files
是唯一可能接收这些类型请求的目录,那么您可以简单地对其进行硬编码。例如:注意:您在与子目录匹配的第二个条件上缺少字符串开头锚点 (
^
)。您需要在测试之前清除浏览器缓存,因为 mod_dir 的错误 301(永久)重定向将被浏览器缓存。
因此,建议首先使用 302(临时)重定向进行测试。
否则,对于更一般的目录解决方案,则需要类似以下内容:
同样的问题也可能适用于RULE#1和RULE#3。请注意,RULE#1 和 RULE#2 本质上是相同的,可以(应该)组合起来。
目前,在发布的规则中,没有什么可以阻止用户直接访问“隐藏”子目录(如果应该知道这一点)。如果用户偶然发现这个“隐藏”子目录,您可能会考虑重定向用户。
另请参阅我对类似问题的回答,该问题更详细(尽管该问题仅涉及一个“网站”) 。
You would seem to have a conflict with mod_dir. If
/files
exists as a physical directory at/192.168.0.6/files
then mod_dir will append the trailing slash with a 301 redirect (in order to "fix" the URL) after the URL has been rewritten, thus exposing the rewritten URL. (You should be seeing a trailing slash on the redirected URL.)You need to manually append the trailing slash before the request is rewritten, by checking whether the URL would map to a directory at the new location.
If
/files
is the only directory that is likely to receive these types of requests then you could simply hardcode this. For example:NB: You were missing the start-of-string anchor (
^
) on the second condition that matches the subdirectory.You need to clear your browser cache before testing since the erroneous 301 (permanent) redirect by mod_dir will have been cached by the browser.
For this reason it is recommended to test first with a 302 (temporary) redirect.
Otherwise, for a more general, any directory solution then something like the following would be required:
The same issue potentially applies to RULE#1 and RULE#3 as well. Note that RULE#1 and RULE#2 are essentially the same and could (should) be combined.
Currently, in the rules as posted, there's nothing stopping a user from accessing the "hidden" subdirectory directly (if this should be known). You might consider redirecting the user if they should stumble upon this "hidden" subdirectory.
See also my answer to a similar question that goes into more detail (although that question is dealing with just one "site").