2016-08-19 178 views
3

當我訪問我的網站https://example.com時,我的瀏覽器響應ERR_CONNECTION_REFUSED。請注意,以下各項工作:如何使用.htaccess將HTTPS非www重定向到HTTPS www?

我如何可以重定向https://example.com請求https://www.example.com使用的.htaccess?

我已經試過這似乎並沒有工作如下:

// .htaccess 

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

回答

1

你的規則是使用2個條件,但你OR之間AND操作。

您可以使用此規則強制執行既wwwhttps

RewriteEngine On 

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

確保在測試前先清除瀏覽器緩存。


編輯:按下面只是https://example.com to https://www.example.com意見重定向您可以使用此規則:

RewriteCond %{HTTP_HOST} !^www\. [NC] 
RewriteRule^https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE] 
+0

我試過這個,但我認爲它與httpd.conf中的現有規則衝突。只有當請求是「https:// example.com」時,是否有重定向到www的方法? – henrywright

+0

如果原始URL是'http:// www.example.com',那麼你不需要成爲'https:// www.example.com'。順便說一句,你可以在httpd.conf中使用這個規則,只記得重新啓動Apache並清空瀏覽器緩存(或使用curl命令行進行測試) – anubhava

+0

感謝您的建議@anubhava。 'http:// www.example.com'已經重定向到'https:// www.example.com'。它只是需要處理的'https:// example.com'到'https:// www.example.com'。 – henrywright

1

你應該工作,因爲它是規則。

ERR_CONNECTION_REFUSED,我猜Apache不會在https(443)端口上偵聽。

但是由於其他一切正常,即使從http://example.com重定向到https://www.example.com,也可能存在其他一些與網絡有關的問題,例如防火牆配置允許https通過www.example.com,但通過example.com阻止。你應該檢查你的服務器提供商。

+0

謝謝,這是有用的知道。我會檢查! – henrywright

相關問題