Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

htaccess force ssl for a particular domain

How do I force SSL for a particular domain currently I 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 is working, however I have several add on domains on my hosting and when I try to access the other add-on domains, the add-on 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 it is giving me an infinite loop.

like image 497
Stone Deft Avatar asked Oct 17 '13 20:10

Stone Deft


2 Answers

This should work:

RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} exampledomain\.org$ [NC]
RewriteRule ^ https://www.examplemydomain.org%{REQUEST_URI}  [R=301,L,NE]
like image 51
anubhava Avatar answered Oct 18 '22 18:10

anubhava


It looks like you're doing 2 different things here. You're adding a www when one is missing, and you're forcing SSL when it isn't used. So there's 2 different conditions and either one being true should force a redirect. That means you want to use the [OR] flag, but the way you were using 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]
like image 2
Jon Lin Avatar answered Oct 18 '22 16:10

Jon Lin