php-.htaccess:ルートパスも変更した場合、ドメインをhttpsにリダイレクトしても機能しません
次のドメインがあります:
example1.com
example2.com
example3.com
ドメインは/public_html/
を指します。 /public_html/ .htaccess
でやりたいことが3つあります:
ドメイン
example2.com
およびexample3.com
をドメイン{に(すべてのパラメーターとパスを使用して)リダイレクトします-code-6}
。-
example1.com
自体は、常にhttps
と{で表示されます(表示されない場合はリダイレクトします)。 -code-9}
は、次のことを意味します:https://www。example1.com
ドメインのカスタムルートパスは
/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
。これは、ルートパスを変更する必要がある最後の4つのコード行を削除する場合にのみ機能します。他のすべてのドメインでは機能しています。
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>
同様の質問
私たちのウェブサイトで同様の質問で答えを見つけてください。