Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

The goal is to have every page (minus one folder) of a site redirect to https. I've discovered the following does not do that. The one folder contains a subdomain. Let's call it

Edit: Modify rules

RewriteEngine On
RewriteBase /

RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www.site1\.mobi [NC,OR]
RewriteCond %{HTTP_HOST} ^site1\.mobi [NC,OR]
RewriteCond %{HTTP_HOST} !^mahogany\.com [NC]
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

If someone types or into their browser, apache with the above in the document root htaccess will not redirect.

What is wrong with the above? Is a line for needed to exclude it?

share|improve this question
up vote 3 down vote accepted

The default logical operator for RewriteCond is "and"; the HTTP_HOST can't be both of those so the condition will never be satisfied.

Try this:

RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www\.site1\.mobi$ [NC,OR]
RewriteCond %{HTTP_HOST} ^site1\.mobi$ [NC]
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

Also, you mentioned a directory exception, but I don't see one in there. You can add one with an extra RewriteCond or by changing your RewriteRule condition:

RewriteRule ^excepted/location/.* https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
share|improve this answer
Thanks Shane. Trying it out now. Did you forget ( & ) in the line 4 of the first code? Originally it was (.*) – David C Jan 13 '13 at 23:35
What about adding this condition: RewriteCond %{HTTP_HOST} !^ [NC] and adding ,OR to the previous rule? – David C Jan 13 '13 at 23:54
@DavidC No, I didn't forget them; you weren't using them so I removed them. And yes, adding that and adding OR to the other hostname will make it apply to all 3 of those hostnames. – Shane Madden Jan 14 '13 at 0:42

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.