2014-09-30 43 views
0

我已經嘗試了幾個解決方案發布在這裏,他們都沒有似乎工作 - 我只是得到重定向錯誤。如何使用.htaccess將所有http流量重定向到https AND www.domain.com

我所試圖做的是所有流量重定向到HTTPS爲:

http://domain.com 
http://www.domain.com 
https://domain.com 
http://alias.com 
http://www.alias.com 
https://alias.com 

都應該去:https://開頭www.domain.com

目前地址開始,如:HTTP: // www不會重定向到https。

這裏是htaccess文件

RewriteEngine On 
RewriteBase/

# Fix Apache internal dummy connections from breaking [(site_url)] cache 
RewriteCond %{HTTP_USER_AGENT} ^.*internal\ dummy\ connection.*$ [NC] 
RewriteRule .* - [F,L] 

RewriteCond %{HTTP_HOST} . 
RewriteCond %{HTTPS} off 
RewriteCond %{HTTP_HOST} ^mydomain\.com [NC] 
RewriteRule ^(.*)$ https://www.mydomain.com/$1 [R=301,L] 

# Exclude /assets and /manager directories from rewrite rules 
RewriteRule ^(manager|assets) - [L] 

# For Friendly URLs 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA] 

RewriteCond %{HTTP_HOST} ^myalias.ca$ [OR] 
RewriteCond %{HTTP_HOST} ^www.myalias.ca$ 
RewriteRule ^/?$ "https\:\/\/mydomain\.com" [R=301,L] 

RewriteCond %{HTTP_HOST} ^myotheralias.ca$ [OR] 
RewriteCond %{HTTP_HOST} ^www.myotheralias.ca$ 
RewriteRule ^/?$ "https\:\/\/mydomain\.com" [R=301,L] 
+0

如果所有請求都應該重定向到https://mydomain.com,爲什麼還要爲HTTP_HOST打擾? – MrTux 2014-09-30 20:56:32

回答

0

你應該擺脫的www重定向在底部:

RewriteCond %{HTTP_HOST} ^myalias.ca$ [OR] 
RewriteCond %{HTTP_HOST} ^www.myalias.ca$ 
RewriteRule ^/?$ "https\:\/\/mydomain\.com" [R=301,L] 

RewriteCond %{HTTP_HOST} ^myotheralias.ca$ [OR] 
RewriteCond %{HTTP_HOST} ^www.myotheralias.ca$ 
RewriteRule ^/?$ "https\:\/\/mydomain\.com" [R=301,L] 

不需要它們,你只需要一個單一的重定向頂部,替換:

RewriteCond %{HTTP_HOST} . 
RewriteCond %{HTTPS} off 
RewriteCond %{HTTP_HOST} ^mydomain\.com [NC] 
RewriteRule ^(.*)$ https://www.mydomain.com/$1 [R=301,L] 

RewriteCond %{HTTPS} off [OR] 
RewriteCond %{HTTP_HOST} !^www\.mydomain\.com$ [NC] 
RewriteRule ^(.*)$ https://www.mydomain.com/$1 [L,R=301] 

這應該照顧所有的重定向。

+0

不 - 這仍然給我一個重定向循環。即使我除了你提供的三條線(除了重寫引擎仍然存在),刪除了所有內容 – 2014-10-01 01:17:43

+0

@SeanKimball除了htaccess文件中還有其他的重定向。規則僅在HTTPS關閉或域名不是www.mydomain.com時纔會重定向。在我的apache測試服務器上,這不會導致循環。 – 2014-10-01 01:49:59

相關問題