簡體   English   中英

301使用www將http重定向到https

[英]301 redirect http to https with www

我必須將我的域從http重定向到https 在我的htaccess中,我已經有。

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

此代碼段將不帶“ www”的所有內容重定向到“ www”。

當我將其更改為

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]

結果是:

http://www.example.com/folder/page.php

becomes

Location => https://www.example.com/folder/page.php

精細!

https://example.com/folder/page.php

becomes

https://www.example.com/folder/page.php

精細!

但:

 http://example.com/folder/page.php

 becomes

 Location => https://example.com/folder/page.php

但這一定是

 Location => https://www.example.com/folder/page.php

如何解決這個問題?

我知道所有這些重定向:

RewriteCond %{SERVER_PORT} !^443$ 
RewriteRule (.*) https://%{HTTP_HOST}/$1   [R=301,L]

RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]

但我只需要一個重定向,而不是兩個301。

您可以使用以下規則

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www [OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$
RewriteRule ^ https://www.%1%{RERUEST_URI} [NE,L,R=301]

在測試之前,請清除瀏覽器緩存。

我在這里找到了解決方案:

https://webmasters.stackexchange.com/questions/84757/htaccess-redirect-non-www-to-www-with-ssl-https

@ w3dk的第二個答案正在起作用。

RewriteEngine On
RewriteCond %{SERVER_PORT} !=443 [OR]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]

您的網站必須同時可以通過SSL訪問www和非www,以觸發.htaccess重定向。

與letsencrypt毫無疑問,擁有這2個證書。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM