htaccess将HTTPS重定向到secure.domain.com,非https到www.domain.com

时间:2012-01-10 20:09:39

标签: apache .htaccess redirect

不确定如何最好地编写以下规则:

  1. 所有安全(HTTPS)流量都应重定向到https://secure.domain.com
  2. 对secure.domain.com的非安全请求应重定向到https://secure.domain.com
  3. 所有其他非安全请求应转至http://www.domain.com
  4. 最后,除“安全”子域外,所有非www请求都应重定向到www。

1 个答案:

答案 0 :(得分:2)

尝试将以下内容添加到域中根文件夹中的htaccess文件中。

RewriteEngine on
RewriteBase /


#Non-secure requests to secure.domain.com should redirect to https://secure.domain.com
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^secure\.domain\.com$ [NC]  
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301] 

#All secure (HTTPS) traffic should redirect to https://secure.domain.com
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} !^secure\.domain\.com$ [NC]  
RewriteRule .* https://secure.domain.com%{REQUEST_URI} [L,R=301] 


#All other non-secure requests should go to http://www.domain.com
#Lastly, all non-www requests should redirect to www, except the "secure" subdomain.
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]  
RewriteRule .* http://www.domain.com%{REQUEST_URI} [L,R=301]