2013-01-23 74 views
3

我的應用程序是在Apache上運行託管在Ubuntu,我想下面再 - 上班的路線:重定向所有流量到www HTTPS

1> http://example.com  -> https://www.example.com 
2> http://www.example.com -> https://www.example.com 
3> https://example.com  -> https://www.example.com 

我使用下列行我默認的Apache配置文件:

RewriteEngine on 
RewriteCond %{HTTP_HOST} !^$ 
RewriteRule ^/?(.*)   https://www.example.com/$1 [L,R] 

這樣,前兩個重定向工作正常,但第三個重定向沒有。第三個可能嗎?如果是的話如何?

謝謝。

+0

認爲它應該是'RewriteRule ^(。*)$ https://www.example.com/$1 [R,L]' – xdevel

+0

我試過了,但它沒有解決我的問題。此外,它將 mysite.com重定向到「https://www.mysite.com//」(兩個尾隨/) – SNAG

+0

Stack Overflow是編程和開發問題的網站。這個問題似乎與題目無關,因爲它不涉及編程或開發。請參閱幫助中心的[我可以詢問哪些主題](http://stackoverflow.com/help/on-topic)。也許[超級用戶](http://superuser.com/)或[Unix&Linux堆棧交換](http://unix.stackexchange.com/)會是一個更好的地方。另請參閱[我在哪裏發佈有關Dev Ops的問題?](http://meta.stackexchange.com/q/134306) – jww

回答

4

我有,我想所有的流量進入虛擬主機去端口443我設立了80端口重定向的虛擬主機定義如下網站:

RewriteEngine on 
RewriteCond %{SERVER_PORT} !^443$ 
RewriteRule ^/?(.*)   https://www.example.com/$1 [L,R] 

這將重定向任何流量進入80到443端口。

確保您的ServerName和ServerAlias設置正確。在這個例子中,你會碰到這樣的:

<VirtualHost *:80> 
    ServerName www.example.com 
    ServerAlias *.example.* example.* example.com 

    <IfModule mod_rewrite.c> 
     RewriteEngine On 
     RewriteCond %{REQUEST_METHOD} ^TRACE 
     RewriteRule .* - [F] 
     RewriteCond %{REQUEST_METHOD} ^TRACK 
     RewriteRule .* - [F] 

     #redirect all port 80 traffic to 443 
     RewriteCond %{SERVER_PORT} !^443$ 
     RewriteRule ^/?(.*) https://www.example.com/$1 [L,R] 
    </IfModule> 

</VirtualHost> 

此之後,你將建立一個443虛擬主機的定義,以及(更少端口重寫部分)。

+0

嗯,這會導致*「頁面沒有正確重定向」*,沒有寫入' error_log'。 – jww

+0

以下是另一個配置選項示例:https://coderwall.com/p/yvg2zw/forward-all-apache-http-traffic-to-https –

+0

無論出於何種原因,您在端口80上的虛擬主機規則會破壞HSTS標頭443虛擬主機。 – jww

0

我知道這已經有一段時間,但我想你可能會尋找這樣的事情:

RewriteCond %{HTTPS_HOST} !^$ 
RewriteRule ^/?(.*)   https://www.mysite.com/$1 [L,R] 

的關鍵部分是在S%{HTTPS_HOST}

你應該能夠把該在你已有的東西之後。

相關問題