Htaccess:除特定域外的所有https

时间:2016-01-07 16:13:17

标签: regex apache .htaccess mod-rewrite redirect

我有以下规则将所有请求重定向到https://www.mainsite.com

RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

现在我想将http://another-domain.com重定向到特定文件夹(博客),但没有https,所以我添加了这个。但是

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?another-domain.com$ [NC]
RewriteCond %{REQUEST_URI} !^/blog/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /blog/$1
RewriteCond %{HTTP_HOST} ^(www.)?another-domain$ [NC]
RewriteRule ^(/)?$ blog/index.php [L]

即使http://another-domain.com仍被重定向到https://www.mainsite.com

1 个答案:

答案 0 :(得分:1)

这样做:

RewriteEngine on

RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?!(www\.)?another-domain\.com$)(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=302,L,NE]

RewriteCond %{HTTP_HOST} ^(www\.)?another-domain\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^((?!blog/).+)$ blog/$1 [L]

RewriteCond %{HTTP_HOST} ^(www\.)?another-domain\.com$ [NC]
RewriteRule ^/?$ blog/index.php [L]