繁体   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