开发者

.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)
Rewr开发者_开发问答iteRule ^(.*)$ 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.


You could add this condition to your rules:

RewriteCond %{SERVER_NAME} =www.example.com


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]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜