Htaccess force ssl for a specific domain

How to force SSL for a specific domain I currently have

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

This works, however, I have several additional domains on my hosting, and when I try to access other additional domains, the additional domains are also forced to use SSL.

I tried this:

 RewriteCond %{HTTP_HOST} ^exampledomain\.org$ [OR] RewriteCond %{HTTP_HOST} ^www\.exampledomain\.org$ RewriteRule ^/?$ "https\:\/\/www\.examplemydomain\.org\/" [R=301,L] 

But that gives me an endless loop.

+7
ssl .htaccess
source share
3 answers

This should work:

 RewriteCond %{HTTPS} off RewriteCond %{HTTP_HOST} exampledomain\.org$ [NC] RewriteRule ^ https://www.examplemydomain.org%{REQUEST_URI} [R=301,L,NE] 
+16
source share

It looks like you are doing 2 different things here. You add www when it is not enough, and you force SSL when it is not used. Thus, there are two different conditions, and one of them is true, must force redirect. This means that you want to use the [OR] flag, but the way to use it breaks if the request is already SSL. Try:

 RewriteCond %{HTTP_HOST} ^exampledomain\.org$ [OR] RewriteCond %{HTTPS} off RewriteRule ^(.*)$ https://www.examplemydomain.org/$1 [R=301,L] 
+2
source share

to try:

 RewriteCond %{HTTPS} off RewriteCond %{HTTP_HOST} ^(www\.){0,1}exampledomain\.org$ RewriteRule ^/?$ "https\:\/\/www\.examplemydomain\.org\/" [R=301,L] 
0
source share

All Articles