Secure SSL and WWW together with htaccess

I try to run the whole site through https and force www.

I saw a number of solutions that provide forcing both www and https, and even several in combination, but I can’t get it to work. I usually end up in a redirect cycle.

The closest I have is the following, but it's still not close enough:

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

I need https://www.example.com/

http://example.com SUCCESS
https://example.com SUCCESS
http://www.example.com FAIL
https://www.example.com SUCCESS , although there is no actual redirect.

thanks

Update
The following code successfully performs the redirect that I need:

 RewriteCond %{ENV:HTTPS} !on [NC] RewriteRule ^(.*)$ https://www.example.com/$1 [R,L] RewriteCond %{ENV:HTTPS} on [NC] RewriteCond %{HTTP_HOST} ^example\.com$ [NC] RewriteRule ^(.*)$ https://www.example.com/$1 [R,L] 
+6
source share
3 answers

Forcing https and www is done by a combination of the other answers provided.

It works:

 RewriteCond %{ENV:HTTPS} !on [NC] RewriteRule ^(.*)$ https://www.example.com/$1 [R,L] RewriteCond %{ENV:HTTPS} on [NC] RewriteCond %{HTTP_HOST} ^example\.com$ [NC] RewriteRule ^(.*)$ https://www.example.com/$1 [R,L] 
+5
source

Try this condition instead, it should force the site in ssl for your domain

 RewriteCond %{ENV:HTTPS} !on [NC] RewriteRule ^(.*)$ https://www.example.com/$1 [R,L] 
+1
source

Try adding the [OR] flag to your conditions. You really don't need either www or https:

 RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC,OR] RewriteCond %{HTTPS} off RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301] 
0
source

Source: https://habr.com/ru/post/921886/


All Articles