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

Weird behavior of RewriteOptions Inherit, InheritDown, InheritBefore, etc when adding www

我想要一个 child .htaccessRewriteEngine on 继承 parent 目录的 RewriteRules.

OK(只有 parent .htaccessRewriteEngine on

条件:

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

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

结果:

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

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

条件:

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

# /article/.htaccess
RewriteEngine on

结果:

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

Parent 规则变得奇怪

条件:

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

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

结果:

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

我建议根本不要使用 Inherit 或类似的选项,而是尽量使用 .htaccess 包含 RewriteEngine on 的文件。

如果您需要在子 .htaccess 文件中包含 RewriteEngine on,您可以自己 re-write 在其中添加所需的规则:

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

(注意最后一行添加的 article/