.htaccess 在省略尾部斜杠时更改 URL

发布于 2025-01-09 22:54:44 字数 652 浏览 5 评论 0原文

我在一个根域中有多个带有 .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 技术交流群。

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

发布评论

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

评论(1

相权↑美人 2025-01-16 22:54:44

<前><代码># 规则#2
RewriteCond %{HTTP_HOST} ^sub\.site1\.org$
RewriteCond %{REQUEST_URI} !/192.168.0.6/
重写规则 ^(.*)$ /192.168.0.6/$1 [L]

当我访问http://sub.site1.org/files/时它可以工作,但是当我访问http://sub.site1.org/files时它可以工作code> (不带斜杠),它将我重定向到 http://sub.site1.org/192.168.0.6/files

您似乎与 mod_dir 有冲突。如果 /files 作为 /192.168.0.6/files 中的物理目录存在,则 mod_dir 将在尾部斜杠处附加 301 重定向(以便“修复”URL) URL 被重写之后,从而暴露了重写的 URL。 (您应该在重定向的 URL 上看到一个尾部斜杠。)

您需要在重写请求之前手动附加尾部斜杠,方法是检查 URL 是否映射到新位置的目录。

如果 /files 是唯一可能接收这些类型请求的目录,那么您可以简单地对其进行硬编码。例如:

# Append slash to "/files"
RewriteCond %{HTTP_HOST} ^sub\.site1\.org$
RewriteRule ^files$ /$0/ [R=301,L]

# RULE#2 (Updated) - Rewrite request to subdirectory
RewriteCond %{HTTP_HOST} ^sub\.site1\.org$
RewriteCond %{REQUEST_URI} !^/192.168.0.6/
RewriteRule (.*) /192.168.0.6/$1 [L]

注意:您在与子目录匹配的第二个条件上缺少字符串开头锚点 (^)。

您需要在测试之前清除浏览器缓存,因为 mod_dir 的错误 301(永久)重定向将被浏览器缓存。

因此,建议首先使用 302(临时)重定向进行测试。

否则,对于更一般的目录解决方案,则需要类似以下内容:

# Append slash to subdirectory BEFORE rewrite
RewriteCond %{HTTP_HOST} ^sub\.site1\.org$
RewriteCond %{REQUEST_URI} !^/192.168.0.6/
RewriteCond %{REQUEST_URI} !\.\w{2,4}$
RewriteCond %{DOCUMENT_ROOT}/192.168.0.6/$1 -d
RewriteRule (.+[^/])$ /$1/ [R=301,L]

# RULE#2 (Updated) - Rewrite request to subdirectory
# (as above)

同样的问题也可能适用于RULE#1RULE#3。请注意,RULE#1RULE#2 本质上是相同的,可以(应该)组合起来。

目前,在发布的规则中,没有什么可以阻止用户直接访问“隐藏”子目录(如果应该知道这一点)。如果用户偶然发现这个“隐藏”子目录,您可能会考虑重定向用户。

另请参阅我对类似问题的回答,该问题更详细(尽管该问题仅涉及一个“网站”) 。

# RULE#2
RewriteCond %{HTTP_HOST} ^sub\.site1\.org$
RewriteCond %{REQUEST_URI} !/192.168.0.6/
RewriteRule ^(.*)$ /192.168.0.6/$1 [L]

When I go to http://sub.site1.org/files/ it works, but when I go to http://sub.site1.org/files (without slash), it redirects me to http://sub.site1.org/192.168.0.6/files

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:

# Append slash to "/files"
RewriteCond %{HTTP_HOST} ^sub\.site1\.org$
RewriteRule ^files$ /$0/ [R=301,L]

# RULE#2 (Updated) - Rewrite request to subdirectory
RewriteCond %{HTTP_HOST} ^sub\.site1\.org$
RewriteCond %{REQUEST_URI} !^/192.168.0.6/
RewriteRule (.*) /192.168.0.6/$1 [L]

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:

# Append slash to subdirectory BEFORE rewrite
RewriteCond %{HTTP_HOST} ^sub\.site1\.org$
RewriteCond %{REQUEST_URI} !^/192.168.0.6/
RewriteCond %{REQUEST_URI} !\.\w{2,4}$
RewriteCond %{DOCUMENT_ROOT}/192.168.0.6/$1 -d
RewriteRule (.+[^/])$ /$1/ [R=301,L]

# RULE#2 (Updated) - Rewrite request to subdirectory
# (as above)

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").

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