在你的代碼:
RewriteCond %{HTTP_HOST} ^domain.com [NC]
它是以下規則的條件;所以該規則將僅適用於無www
請求。
此外,在測試新代碼時不要使用R=301
重定向,除非您確定規則正常,並且您可以使用R=302
或僅使用R
。
不管你的代碼將下面的代碼在你的主目錄.htaccess
的:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule^https://%{HTTP_HOST}%{REQUEST_URI} [L,R]
如果要強制每個請求是www
你應該這樣做:
RewriteEngine On
RewriteCond %{HTTPS} !=on
#this line above will match any request without `https`
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule^https://%{HTTP_HOST}%{REQUEST_URI} [L,R]
#the two lines above will exclude none `www` request and force only `www` request unto `https` and the rule will stop here `L`
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule^https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R]
# the last two line will exclude any `www` request and force only none `www` into `https`
這爲我工作[非www到https www重定向](https://stackoverflow.com/questions/17453412/redirect-to-http-non-www-to-https-www-htaccess/31234149#31234149) –