Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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 mahogany.com.

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 site1.mobi or www.site1.mobi 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 mahogany.com needed to exclude it?

share|improve this question

1 Answer 1

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} !^mahogany.com [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

 
discard

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.