2015-05-08 148 views
2

我如何獲得在htaccess強制https & www。在域上,& http沒有www在子域上?htaccess force https&www。在域名&http沒有WWW子域

下重定向到http://sub.domain.comhttps://domain.com

# for main domains 
RewriteCond %{HTTPS} !=on [OR] 
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC] 
RewriteRule ^.*$ https://www.domain.com%{REQUEST_URI} [R,L] 


# for sub domain 
RewriteCond %{HTTP_HOST} ^(www\.)?sub\.domain\.com$ [NC] 
RewriteCond %{HTTPS} on [OR] 
RewriteCond %{HTTP_HOST} ^www\. [NC] 
RewriteRule^http://sub.domain.com%{REQUEST_URI} [R=301,L,NE] 

回答

3

您可以使用:

# for main domains 
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC] 
RewriteCond %{HTTPS} off [OR] 
RewriteCond %{HTTP_HOST} !^www\. [NC] 
RewriteRule^https://www.domain.com%{REQUEST_URI} [R=301,L,NE] 

# for sub domain 
RewriteCond %{HTTP_HOST} ^(www\.)?sub\.domain\.com$ [NC] 
RewriteCond %{HTTPS} on [OR] 
RewriteCond %{HTTP_HOST} ^www\. [NC] 
RewriteRule^http://sub.domain.com%{REQUEST_URI} [R=301,L,NE] 

你需要清除瀏覽器緩存後進行測試。

+1

你!你拯救了人類免受外星人襲擊!謝謝。 –

相關問題