[英]301 redirect http to https with www
I have to redirect my domain from http to https . 我必须将我的域从http重定向到https 。 In my htaccess I have already.
在我的htaccess中,我已经有。
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
This snippet redirect everything without "www" to "www". 此代码段将不带“ www”的所有内容重定向到“ www”。
When I change this to 当我将其更改为
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
The result is: 结果是:
http://www.example.com/folder/page.php
becomes
Location => https://www.example.com/folder/page.php
Fine! 精细!
https://example.com/folder/page.php
becomes
https://www.example.com/folder/page.php
Fine! 精细!
but: 但:
http://example.com/folder/page.php
becomes
Location => https://example.com/folder/page.php
but it has to be 但这一定是
Location => https://www.example.com/folder/page.php
How is it possible to fix this? 如何解决这个问题?
I know all of this redirects: 我知道所有这些重定向:
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]
but I need only one redirection instead of two 301. 但我只需要一个重定向,而不是两个301。
You can use the following rule 您可以使用以下规则
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www [OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$
RewriteRule ^ https://www.%1%{RERUEST_URI} [NE,L,R=301]
Clear your browser cache before testing this. 在测试之前,请清除浏览器缓存。
I have found a solution here: 我在这里找到了解决方案:
https://webmasters.stackexchange.com/questions/84757/htaccess-redirect-non-www-to-www-with-ssl-https https://webmasters.stackexchange.com/questions/84757/htaccess-redirect-non-www-to-www-with-ssl-https
The second answer by @w3dk is working. @ w3dk的第二个答案正在起作用。
RewriteEngine On
RewriteCond %{SERVER_PORT} !=443 [OR]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
Your site needs to be accessible by both www and non-www over SSL for the .htaccess redirect to trigger. 您的网站必须同时可以通过SSL访问www和非www,以触发.htaccess重定向。
With letsencrypt its no problem to have this 2 certs. 与letsencrypt毫无疑问,拥有这2个证书。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.