[英].htaccess: Redirect domain to https not working if also changing the root path
我有以下域:
example1.com
example2.com
example3.com
域指向/public_html/
。 我想在/public_html/.htaccess
做三件事:
將域example2.com
和example3.com
(以及所有參數和路徑)重定向到域example1.com
。
example1.com
本身應始終通過https
和www
顯示(如果沒有,則重定向),這意味着: https://www.example1.com
: https://www.example1.com
域的自定義根路徑是/public_html/
。 我想將其更改為/public_html/example_path/
。
我在/public_html/.htaccess
有以下/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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.