Apache mod_rewrite:在一个子域中强制使用HTTPS,并强制HTTP用于另一个子域

时间:2018-02-06 14:19:36

标签: apache .htaccess https

我有这段代码,但效果不好: 我希望omly HTTP到a-subdomain,只有HTTPS到subdomain ....

使用基于@Dusan Bajic的更改代码进行编辑:

#Trying to force https, this works...
RewriteCond %{HTTPS} !=on [NC]
RewriteCond %{HTTP_HOST} !^a-subdomain\.domain\.com$ [NC]
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [NC,R=301,L]

#Trying to force http, not works...
RewriteCond %{HTTPS} =on [NC]
RewriteCond %{HTTP_HOST} ^a-subdomain\.domain\.com$ [NC]
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [NC,R=301,L]
  

子域名重定向到HTTPS - 没关系。

     

可以通过HTTP访问a-subdomain - 没关系

     

a-subdomain继续通过HTTPS访问 - 确定。

1 个答案:

答案 0 :(得分:0)

使用RewriteCond语法:

RewriteCond TestString CondPattern [flags]

CondPattern(在没有任何前缀的情况下使用时)是perl兼容的正则表达式。 以=为前缀,CondPattern作为普通字符串处理,并按字典顺序与TestString

进行比较

使用:

RewriteCond %{HTTP_HOST} =a-subdomain.domain.com

RewriteCond %{HTTP_HOST} ^a-subdomain\.domain\.com$