2013-07-03 136 views
5

我想從任何方向使用HTTPS協議重定向到我們的站點,但有些重定向它不起作用。我想這一點:重定向到HTTP非www到HTTPS www htaccess

  • http://www.site.co TO https://www.site.co
  • http://site.co TO https://www.site.co

這是我的htaccess:

RewriteEngine On 
RewriteCond %{HTTPS} off 
RewriteRule ^(.*)$ https://www.domain.com/$1 [L,R=301] 

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

它不工作的第二條規則。它會轉到我們網站的另一個方向,而不是重定向到HTTPS網站。

+0

[歡迎使用stackoverflow,請考慮閱讀** SO Tour **。它會引導你如何最好地使用這個網站](http:// stackoverflow。com/about) – Prix

+0

[htaccess重定向非www到www,同時保留http和https]的可能重複(http://stackoverflow.com/questions/6525348/htaccess-for-redire--non-www-to-www- while-preserving-http-https) – Prix

+0

感謝評論@Prix。我嘗試了這種迴應,但並沒有爲我工作。我不知道實際上有什麼問題 –

回答

29

試試這樣說:

RewriteEngine On 

RewriteCond %{HTTP_HOST} !^www\. [NC] 
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301] 

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

這裏唯一的區別是,首先,我們從非WWW重定向到WWW然後我們檢查HTTPS,並將其重定向。

如果它不工作,那麼試試這個:通過大獎賽

RewriteEngine On 
RewriteCond %{HTTP_HOST} !^www\. [NC] 
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301] 
RewriteCond %{HTTPS} !=on 
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301] 
+0

嗨@Prix,感謝您的回答,但不幸的是仍然沒有爲我工作。也許這可能是配置虛擬主機的問題。它可能是??? –

+0

@AnibalMauricio是否確定你的htaccess或PHP上沒有其他東西會導致這種情況? – Prix

+0

好吧,我只是想通了,那個頁面是apache的默認頁面,所以它的問題一定是虛擬主機的配置吧? –

0

如果你正在使用One.com爲您的虛擬主機提供商,你應該使用下面的代碼,而不是作品。爲了使它更加動態,可以使用SERVER_NAME和REQUEST_URI而不是靜態域名。

RewriteEngine On 
#we replace domain.com/$1 with %{SERVER_NAME}%{REQUEST_URI}. 
RewriteCond %{HTTP_HOST} !^www\. [NC] 
RewriteRule ^(.*) https://www.%{SERVER_NAME}%{REQUEST_URI} [L,R=301] 

#here we dont use www as non www was already redirected to www. 
RewriteCond %{HTTPS} off 
RewriteRule ^(.*) https://%{SERVER_NAME}%{REQUEST_URI} [L,R=301] 
12

答案:

RewriteEngine On 

RewriteCond %{HTTP_HOST} !^www\. [NC] 
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301] 

RewriteCond %{SERVER_PORT} !^443$ 
RewriteRule ^(.*)$ https://www.domain.com/$1 [L,R=301] 
+0

但是,如果我的網址是「https:// example.com/page」不會重定向到WWW。有任何想法嗎? –

+0

'http:// example.com/page'成功重定向到www。 –

0

這都會用到WWW或非www 如果嘗試打開以www鏈接URL,然後重定向到HTTPS以www

Example : http://domain.com redirect to https://domain.com 

或者如果您嘗試打開鏈接與非WWW然後URL重定向到https與非www

Example : http://www.domain.com redirect to https://www.domain.com 

RewriteEngine on 

RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L] 
-1
RewriteEngine On 

RewriteCond %{HTTP_HOST} !^www\. [NC] 
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301] 

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

對不起,我沒有足夠的點評論,但@prix規則將帶來不必要的重定向。

RewriteEngine On 

RewriteCond %{HTTP_HOST} !^www\. [NC] 
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301] 

RewriteCond %{SERVER_PORT} !^443$ 
RewriteRule ^(.*)$ https://www.domain.com/$1 [L,R=301] 

您可以嘗試在GTMETRIX http://domain.com/,你會得到這個消息

"Avoid landing page redirects for the following chain of redirected URLs." 

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

爲了防止這種情況,轉到第一個重寫規則,並在http後面添加一個「S」。這套新規則看起來就像這樣:

RewriteEngine On 

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

RewriteCond %{SERVER_PORT} !^443$ 
RewriteRule ^(.*)$ https://www.domain.com/$1 [L,R=301] 
-1

例如https://example.com應該去https://www.example.com

RewriteEngine On 
RewriteCond %{SERVER_PORT} !=443 
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC] 
RewriteRule ^(.*)$ "https\:\/\/www\.example\.com\/$1" [R=301,L] 

注意 - 你應該改變example.com你自己的域名。

+4

我不確定這是什麼增加了現有的答案 - 但是有什麼理由,你不能只使用example.com和www.example.com? –

相關問題