Stone Deft
Stone Deft

Reputation: 85

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.

Upvotes: 8

Views: 5250

Answers (3)

Jon Lin
Jon Lin

Reputation: 143886

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]

Upvotes: 2

anubhava
anubhava

Reputation: 785128

This should work:

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

Upvotes: 17

Iłya Bursov
Iłya Bursov

Reputation: 24146

try:

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

Upvotes: 0

Related Questions