.htaccess重写强制HTTPS,如何避免一些域名?

时间:2017-06-15 15:00:29

标签: .htaccess mod-rewrite

我想为我的某个网站强制使用HTTPS。不幸的是,我的服务器上有其他网站,我不想将它们重定向到https。我试图添加重写条件,但它不起作用。你知道为什么吗?我的目标是避免使用botletter.com的https重定向

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} !http://botletter.com [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP:X-Forwarded-Proto} !https 
RewriteCond %{HTTP_HOST} !http://www.botletter.com [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

谢谢。

1 个答案:

答案 0 :(得分:1)

您可以使用:

RewriteEngine on

# To https www
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} !(botletter\.com|eedart\.co|other\.com) [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]

# To https
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !(botletter\.com|eedart\.co|other\.com) [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]

# botletter to www.botletter
RewriteCond %{HTTP_HOST} ^(botletter\.com|eedart\.co|other\.com) [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]

根据您的配置,您可能需要使用:
RewriteCond %{HTTP:X-Forwarded-Proto} !https代替RewriteCond %{HTTPS} off