2013-01-23 73 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}

你应该能够把该在你已有的东西之后。

相关问题