将子域重定向到文件夹时,如何避免带有尾部斜杠的 URL 的文件夹子域重复

发布于 2025-01-18 00:34:13 字数 707 浏览 1 评论 0原文

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

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

发布评论

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

评论(1

烟花肆意 2025-01-25 00:34:14

是的,这是由于 mod_dir 在重写发生后在目录中附加了一个斜杠(带有 301 重定向),从而暴露了内部重写的 URL/目录。

因此,规范 URL 需要是 /articles/ (带有尾部斜杠),而不是 /articles。我们可以在重写发生之前通过外部重定向来纠正此问题。

(这可以避免您必须禁用 DirectorySlash - 这仍然会给您带来规范化/重复内容问题。)

例如,在现有重写之前,测试以查看请求的 URL 路径(该缺少尾部斜杠)作为目录存在于 /sub 目录中,如果是这种情况,请附加斜杠。

# Redirect to append trailing slash if exists as a dir inside "/sub"
RewriteCond %{HTTP_HOST} ^sub\.mydomain\.com [NC]
RewriteCond %{DOCUMENT_ROOT}/sub/$1 -d
RewriteRule ^((?!sub/).*[^/])$ /$1/  [R=301,L]

作为一项额外的优化,您可以通过排除看起来具有文件扩展名的 URL,避免对静态资源(自然不会以尾部斜杠结尾)执行不必要的文件系统检查(相对昂贵)。 (这假设您没有具有文件扩展名的物理目录,例如 /sub/somedir.xyz

添加以下内容作为第二个条件(之前< /em> 文件系统检查)在上述规则中:

RewriteCond %{REQUEST_URI} !\.\w{2,4}$

旁白:

RewriteCond %{HTTP_HOST} ^sub.mydomain.com$ [NC]
RewriteRule ^((?!sub).*)$ /sub/$1 [NC]

您可能应该在此 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.

# Redirect to append trailing slash if exists as a dir inside "/sub"
RewriteCond %{HTTP_HOST} ^sub\.mydomain\.com [NC]
RewriteCond %{DOCUMENT_ROOT}/sub/$1 -d
RewriteRule ^((?!sub/).*[^/])$ /$1/  [R=301,L]

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:

RewriteCond %{REQUEST_URI} !\.\w{2,4}$

Aside:

RewriteCond %{HTTP_HOST} ^sub.mydomain.com$ [NC]
RewriteRule ^((?!sub).*)$ /sub/$1  [NC]

You should probably be using the L flag on this RewriteRule directive. (And the NC flag should be unnecessary.)

The regex ^((?!sub).*)$ excludes any URL-path that simply starts sub, 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.

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