views:

30

answers:

2

I have this .htaccess file but I would like it to only do this when I'm on the live site.

Is there any way I can:

// if server_name looks like example.com to this
// else dont run this bit
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} (auth|register|secure|payment|admin|trading_careers)
RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]

RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(auth|register|secure|payment|admin|trading_careers)
RewriteRule ^(.*)$ http://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]

I've tried working with the SetEnvIfNoCase and IfDefine with no success. Don't really know if it's possible to do what I want.

Thank you in advance.

EDIT (solution example for future readers):

// if https is off
RewriteCond %{HTTPS} off
// if server_name like example.com (case insensitive) OR
RewriteCond %{SERVER_NAME} =example.com [NC,OR]
// server_name like www.example.com (case insensitive)...
RewriteCond %{SERVER_NAME} =www.example.com [NC]
RewriteCond %{REQUEST_URI} (auth|register|secure|payment|admin|trading_careers)
RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]

Thank you for the great help.

+1  A: 

You could add this condition to your rules:

RewriteCond %{SERVER_NAME} =www.example.com
Gumbo
@Marcus Adams: No, I meant *SERVER\_NAME* as Frankie requested.
Gumbo
@Marcus Adams: `ServerName` is also allowed in the `<VirtualHost>` context.
Gumbo
@Gumbo right in front of my eyes... how could I not see it! ;) Thank you so much!
Frankie
@Marcus Adams: The leading `=` was not a mistake but on purpose. It denotes to use simple string comparison instead of a regular expression comparison.
Gumbo
+1  A: 

If you use a different hostname for the live site:

Just check the HTTP_HOST on each condition also:

RewriteCond %{HTTP_HOST} www.example.com [NC]
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} (auth|register|secure|payment|admin|trading_careers)
RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
Marcus Adams
@Marcus, thank you for the input. Both of you should have gotten the correct answer but Marcus was more on target with the SERVER_NAME. I also believe SERVER_NAME should be used always in detriment to HTTP_HOST as HTTP_HOST comes from the client (right)?
Frankie
Holy crap. I have to go change all my .htaccess to SERVER_NAME.
Marcus Adams