2015-06-02 130 views
0

我在Centos 6.6上運行Apache/2.2.15,並使用StartCom提供的免費證書。我的主頁文件是/var/www/index.php,所以我創建了一個文件/var/www/.htaccess,內容如下,建議爲here.htaccess未將網站重定向到https

RewriteEngine On 
# This will enable the Rewrite capabilities 

RewriteCond %{HTTPS} !=on 
# This checks to make sure the connection is not already HTTPS 

RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L] 
# This rule will redirect users from their original location, to the same location but using HTTPS. 
# i.e. http://www.example.com/foo/ to https://www.example.com/foo/ 
# The leading slash is made optional so that this will work either in httpd.conf 
# or .htaccess context 

然而,在URL框中輸入

myWebSite.com 

帶來了我的http協議的網站。如果我輸入

https://myWebSite.com 

改爲,我在https協議中獲取我的網站。我的目標是,簡單地輸入

myWebSite.com 

,我看不出爲什麼.htaccess文件沒有影響,要得到我的https協議的網站。

+0

你確定你的.htaccess正在被讀取嗎?你的配置中有'AllowOverride All'嗎? –

+0

修復了原始問題。現在,當我進入mywebsite.com時,我得到「該頁面沒有正確重定向,Firefox檢測到服務器正以一種永遠不會完成的方式重定向該地址的請求。」我以前沒有遇到過這個問題。我的.htaccess文件有問題嗎?謝謝, – OtagoHarbour

+0

您在嘗試我的答案之前或之後是否有此錯誤? –

回答

1

看來你的.htaccess文件沒有被讀取。所以確保你的配置中有AllowOverride All

也爲您的規則,我不會使用SERVER_NAME,這並不總是設置,有時不正確。我會使用HTTP_HOST變量或您的實際域名。你也應該指定301作爲你的重定向,因爲沒有它302是默認的。你希望這是一個永久的重定向。

RewriteEngine On 
# This will enable the Rewrite capabilities 
RewriteCond %{HTTPS} !^on [OR] 
# This checks to make sure the connection is not already HTTPS 
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC] 
RewriteRule ^/?(.*)$ https://example.com/$1 [R=301,L] 

我也使得它會刪除www因爲你不顯示您正在使用它。