.htaccess if / else depending on {SERVER_NAME}?

I have this .htaccess file, but I would like it to be done only when I am on the site.

Is there any way:

// 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 tried working with SetEnvIfNoCase and IfDefine without success. I don’t know if I can do what I want.

Thanks in advance.

EDIT (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 your great help.

+4
source share
2 answers

You can add this condition to your rules:

 RewriteCond %{SERVER_NAME} =www.example.com 
+3
source

If you use a different hostname for the site:

Just check HTTP_HOST and for each condition:

 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] 
+1
source

Source: https://habr.com/ru/post/1313894/


All Articles