.htaccess:如果同时更改根路径,则将域重定向到 https 不起作用



我有以下域:

example1.com
example2.com
example3.com

域指向/public_html/。我想在/public_html/.htaccess做三件事:

  1. 重定向(使用所有参数和路径)域example2.comexample3.com到域example1.com

  2. example1.com本身应该始终显示(如果没有,则重定向)与httpswww,表示:https://www.example1.com

  3. 域的自定义根路径为/public_html/。我想将其更改为/public_html/example_path/.

我有以下/public_html/.htaccess

RewriteCond %{HTTP_HOST} ^example2.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www.example2.com [NC]
RewriteRule ^(.*)$ https://www.example1.com/$1 [L,R=301,NC]
RewriteCond %{HTTP_HOST} ^example3.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www.example3.com [NC]
RewriteRule ^(.*)$ https://www.example1.com/$1 [L,R=301,NC]
RewriteCond %{HTTP_HOST} ^example1.com [NC]
RewriteRule ^(.*)$ https://www.example1.com/$1 [L,R=301,NC]
RewriteCond %{HTTP_HOST} ^example1.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.example1.com$
RewriteCond %{REQUEST_URI} !example_path/
RewriteRule (.*) /example_path/$1 [L]

这几乎按预期工作。但是打开http://www.example1.com时,没有重定向到https://www.example1.com。这仅在删除最后四行代码时有效,这应该会更改根路径。

对于所有其他域,它都在工作。

为什么不为http://www.example1.com而醒来?为什么它只在不更改根路径时才有效?

将此规则添加到 .htaccess 文件顶部

# http <> https
RewriteCond %{HTTPS} off
RewriteRule (.*) https://www.example1.com/$1 [R=301,L]

或者检查这些重写而不是您的规则:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://www.example1.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} !^www.example1.com [NC]
RewriteRule (.*) https://www.example1.com/$1 [R=301,L]
RewriteCond %{REQUEST_URI} !example_path/
RewriteRule (.*) /example_path/$1 [L]
</IfModule>

对于特定域(www/non-www example-other.co、www/non-www example3.com、www/non-www example2.com、example1.com)

<IfModule mod_rewrite.c>
RewriteEngine On
# http www/non-www example-other.com/any to https://www.example-other.com/any
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(www.)?example-other.com [NC]
RewriteRule (.*) https://www.example-other.com/$1 [R=301,L]
# https non-www example-other.com/any to https://www.example-other.com/any
RewriteCond %{HTTP_HOST} ^example-other.com [NC]
RewriteRule (.*) https://www.example-other.com/$1 [R=301,L]
# http www/non-www example1.com/any to https://www.example1.com/any
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(www.)?example1.com [NC]
RewriteRule (.*) https://www.example1.com/$1 [R=301,L]
# http/https www/non-www example2.com/any to https://www.example1.com/any
RewriteCond %{HTTP_HOST} ^(www.)?example2.com [NC]
RewriteRule (.*) https://www.example1.com/$1 [R=301,L]
# http/https www/non-www example3.com/any to https://www.example1.com/any
RewriteCond %{HTTP_HOST} ^(www.)?example3.com [NC]
RewriteRule (.*) https://www.example1.com/$1 [R=301,L]
# https non-www example1.com/any to https://www.example1.com/any
RewriteCond %{HTTP_HOST} ^example1.com [NC]
RewriteRule (.*) https://www.example1.com/$1 [R=301,L]
# Rewrite /any to /example_path/any 
RewriteCond %{REQUEST_URI} !example_path/
RewriteRule (.*) /example_path/$1 [L]
</IfModule>

最新更新