添加 www 时 RewriteOptions Inherit、InheritDown、InheritBefore 等的奇怪行为

发布于 2025-01-09 07:29:03 字数 1191 浏览 2 评论 0原文

我想要一个带有 RewriteEngine on 的子 .htaccess ,它继承父目录的 RewriteRule

好的(仅限父 .htaccessRewriteEngine 打开

条件:

# /.htaccess
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L,QSA]

# /article/.htaccess
(Empty or without RewriteEngine on)

结果:

example.com/article/34 => www.example.com/article/34

父规则被忽略(至少在某些情况下)

条件:

# /.htaccess
RewriteEngine on
RewriteOptions InheritDown #or InheritDownBefore
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L,QSA]

# /article/.htaccess
RewriteEngine on

结果:

example.com/article/34 => example.com/article/34

父规则变得奇怪

条件:

# /.htaccess
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L,QSA]

# /article/.htaccess
RewriteEngine on
RewriteOptions Inherit #or InheritBefore

结果:

example.com/article/34 => www.example.com/34

I want to have a child .htaccess with RewriteEngine on that inherits parent directory's RewriteRules.

OK (only parent .htaccess with RewriteEngine on)

Condition:

# /.htaccess
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L,QSA]

# /article/.htaccess
(Empty or without RewriteEngine on)

Result:

example.com/article/34 => www.example.com/article/34

Parent rule ignored (at least in some cases)

Condition:

# /.htaccess
RewriteEngine on
RewriteOptions InheritDown #or InheritDownBefore
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L,QSA]

# /article/.htaccess
RewriteEngine on

Result:

example.com/article/34 => example.com/article/34

Parent rule becomes wierd

Condition:

# /.htaccess
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L,QSA]

# /article/.htaccess
RewriteEngine on
RewriteOptions Inherit #or InheritBefore

Result:

example.com/article/34 => www.example.com/34

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

梦太阳 2025-01-16 07:29:03

我建议根本不要使用 Inherit 或类似选项,而是尝试在 RewriteEngine on 上使用尽可能少的 .htaccess 文件。

如果您需要在子.htaccess文件中使用RewriteEngine on,您可以自己在其中重写所需的规则:(

# /article/.htaccess
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/article/$1 [R=301,L,QSA]

注意添加的article/< /code> 最后一行)

I suggest not to use Inherit or similar options at all and instead, try to have as few .htaccess files with RewriteEngine on as possible.

If you needed to have a RewriteEngine on in a child .htaccess file, you can re-write desired rules in it yourself:

# /article/.htaccess
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/article/$1 [R=301,L,QSA]

(Note the added article/ at last line)

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