Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.htaccess if/else depending on {SERVER_NAME}?

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.

like image 829
Frankie Avatar asked Jun 25 '10 17:06

Frankie


1 Answers

You could add this condition to your rules:

RewriteCond %{SERVER_NAME} =www.example.com
like image 69
Gumbo Avatar answered Sep 21 '22 14:09

Gumbo