重定向到https,但只限定特定的域/子域和特定网址

时间:2017-07-05 21:16:53

标签: regex apache .htaccess redirect

我想将所有流量从HTTP重定向到HTTPS,但前提是特定网址与/ oldsite不同(但仅限于subdomains sub1和sub2)

例如:

http://example.com/any/thing --> redirects to --> https://example.com/any/thing

http://sub1.example.com/any/thing --> redirects to --> https://sub1.example.com/any/thing

http://example.com/oldsite/any/thing 

http://sub1.example.com/oldsite/any/thing -> do not cause this redirect

我试过了:

RewriteEngine on
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:(sub1|sub2)\.)?example\.com 
RewriteCond %{REQUEST_URI} !^/oldsite [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

谢谢。

更新:还测试了最后两行:

RewriteCond %{THE_REQUEST} !/oldsite [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301, NE]

1 个答案:

答案 0 :(得分:0)

确保在HTTPS关闭时使用您的规则,并使用THE_REQUEST变量代替REQUEST_URI

RewriteCond %{HTTPS} off
RewriteCond %{THE_REQUEST} !/oldsite [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]