2016-05-17 62 views
0

我想如果域匹配非www到www所有域非HTTPS重定向到https。htaccess的條件重定向如果域匹配

這裏是我的代碼,它的工作原理除了它不會將非www重定向到僅限https版本的www。

注意:我分離非www和www版本的規則,因爲如果我在url中添加www,它會廣告www 2 x。

RewriteEngine On 

#all non-www to www (without HTTPS) 
RewriteCond %{HTTP_HOST} !^www\. 
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L] 

#non-https to https if these 2 domains matched - and since there is already www don't add it. 
RewriteCond %{HTTP_HOST} ^www\.domainA\.com [NC,OR] 
RewriteCond %{HTTP_HOST} ^www\.domainB\.com [NC] 
RewriteCond %{HTTPS} off 
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L] 

#non-https to https if these 2 domains matched - and also www 
RewriteCond %{HTTP_HOST} ^domainA\.com [NC,OR] 
RewriteCond %{HTTP_HOST} ^domainB\.com [NC] 
RewriteCond %{HTTPS} off 
RewriteRule (.*) https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L] 

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_URI} !index 
RewriteRule (.*)$ index.html 
+0

什麼不起作用? – anubhava

+0

@anubhava如果我訪問https:domainA.com它不會將www添加到它。 –

回答

0

嘗試添加這個,它應該添加www。 https://

RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$ 
RewriteCond %{HTTPS}s ^on(s)| 
RewriteRule^http%1://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301] 
0

代碼很好。問題在於www未在主機中配置。