Question

I have been using this in order to force usage of HTTPS:

RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

And this works perfectly fine.

How can I modify this rule so that it also forces a www. prefix for the domain and SSL? Perhaps it can be done right within the RewriteRule line above? That would be best. But I don't think it can be done, because of the condition applying only to non-HTTPS calls?

Unfortunately my knowledge of Apache configuration and its syntax goes way below zero.

Was it helpful?

Solution

Will this work for you?

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

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

This is shorter actually.

RewriteCond %{HTTP_HOST} ^example\.com [NC,OR]
RewriteCond %{HTTPS} !^on$
RewriteRule (.*) https://www.example.com/$1 [R=301,L]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top