将子域重定向到文件夹时,如何避免带有尾部斜杠的 URL 的文件夹子域重复
我对Apache 2.4上的内部MOD_REWRTE重定向有一个奇怪的问题。
我将子域sub
重定向到文件夹/sub
带有以下指令:
RewriteCond %{HTTP_HOST} ^sub.mydomain.com$ [NC]
RewriteRule ^((?!sub).*)$ /sub/$1 [NC]
在我的.htaccess
文件中, > https://sub.mydomain.com/articles/ - URL在浏览器的地址字段中保留下来,并且正如预期的那样,位于/ssub/articles/index.html 是服务/
但是,当我在浏览器中输入https://sub.mydomain.com/articles
(注意缺失的slash)浏览器中的URL更改为https:https:https:https: //sub.mydomain.com/sub/articles/
(请注意重复的sub
作为文件夹和 subdomain!)。
我猜这是由于Apache的默认行为引起的,它以 extern 重定向的斜线目录请求添加了斜线。我可以增加斜线,但我当然想避免使用文件夹辅助辅助辅助工具。 - 我该怎么做?
I have a strange problem with internal mod_rewrte redirects on Apache 2.4.
In my .htaccess
file I redirect a subdomain sub
to a folder /sub
with the following directives:
RewriteCond %{HTTP_HOST} ^sub.mydomain.com$ [NC]
RewriteRule ^((?!sub).*)$ /sub/$1 [NC]
Thos works perfectly for, say, https://sub.mydomain.com/articles/
- the URL stays like this in the browser's address field and as expected the data from the location at /sub/articles/index.html
are served/
However when I type in in the browser https://sub.mydomain.com/articles
(note the missing slash) the URL is changed in the browser to https://sub.mydomain.com/sub/articles/
(note the duplicated sub
as folder and subdomain!).
I guess this is caused by Apache's default behavior to add a slash to slashless directory requests as external redirects. The adding of the slash is OK with me, but of course I want to avoid the folder-subdomain duplication. - How can I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,这是由于 mod_dir 在重写发生后在目录中附加了一个斜杠(带有 301 重定向),从而暴露了内部重写的 URL/目录。
因此,规范 URL 需要是
/articles/
(带有尾部斜杠),而不是/articles
。我们可以在重写发生之前通过外部重定向来纠正此问题。(这可以避免您必须禁用
DirectorySlash
- 这仍然会给您带来规范化/重复内容问题。)例如,在现有重写之前,测试以查看请求的 URL 路径(该缺少尾部斜杠)作为目录存在于
/sub
目录中,如果是这种情况,请附加斜杠。作为一项额外的优化,您可以通过排除看起来具有文件扩展名的 URL,避免对静态资源(自然不会以尾部斜杠结尾)执行不必要的文件系统检查(相对昂贵)。 (这假设您没有具有文件扩展名的物理目录,例如
/sub/somedir.xyz
)添加以下内容作为第二个条件(之前< /em> 文件系统检查)在上述规则中:
旁白:
您可能应该在此
RewriteRule
指令上使用L
标志。 (并且NC
标志应该是不必要的。)正则表达式
^((?!sub).*)$
排除任何仅开始sub< 的 URL 路径/code>,其中包括
/subfoo
和/subbar
等(这自然会阻止在/sub
目录中访问这些目录)。任何有效的请求都会以/sub/
开头(带有尾部斜杠),因此应该包含在否定前瞻中,就像我在上面的规则中所做的那样。如果还没有,如果应该公开/发现此目录,请考虑重定向以从直接请求中删除
/sub/
。Yes, this is caused by mod_dir appending a slash (with a 301 redirect) to the directory after the rewrite has occurred, exposing the internally rewritten URL/directory.
The canonical URL therefore needs to be
/articles/
(with a trailing slash), not/articles
. We can correct this with an external redirect before the rewrite occurs.(This avoids you having to disable the
DirectorySlash
- which would still leave you with a canonicalization / duplicate content issue.)For example, before the existing rewrite, test to see if the requested URL-path (that is missing a trailing slash) exists as a directory in the
/sub
directory and append a slash if that is the case.As an additional optimisation, you can avoid unnecessarily performing a filesystem check (which are relatively expensive) on static assets (that naturally do not end in a trailing slash) by excluding URLs that look like they have a file-extension. (This assumes you don't have physical directories that have, what looks like, a file extension, eg.
/sub/somedir.xyz
)Add the following as the 2nd condition (before the filesystem check) in the above rule:
Aside:
You should probably be using the
L
flag on thisRewriteRule
directive. (And theNC
flag should be unnecessary.)The regex
^((?!sub).*)$
excludes any URL-path that simply startssub
, which would include/subfoo
and/subbar
, etc. (which naturally prevents these directories from being accessible in the/sub
directory). Any valid request would start/sub/
(with a trailing slash), so should be included in the negative lookahead, as I did in the rule above.If not already, consider also redirecting to remove
/sub/
from direct requests if this directory should be exposed/discovered.