用www强制所有域名https

Vaz*_*lly 7 .htaccess ssl https

我在这个论坛上尝试了12个不同的解决方案,其中没有解决方案.我希望我的所有域名都有https:// www.

现在我用这个:

 RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule (.*) https://www.%{HTTP_HOST}%{REQUEST_URI}
Run Code Online (Sandbox Code Playgroud)

但现在当我访问www.example.com时,它会重定向到https://www.www.example.com (两次www.)

http://example.com完美无瑕地重定向到https://www.example.com

Jus*_*man 10

这是正常的行为.
实际上,www在进行重定向之前,您需要检查是否在主机中.

一种简单的方法是在两个条件下分解问题

RewriteEngine on

# redirect http://www.example.com to https://www.example.com
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

# redirect http(s)://example.com to https://www.example.com
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Run Code Online (Sandbox Code Playgroud)