Apache 重写重定向到 https 和 www

Apache 重写重定向到 https 和 www

我需要让 mod_rewrite 执行以下操作

http://abc.com -> https://www.abc.com
https://abc.com/login -> https://www.abc.com/login
http://www.abc.com?x=3 -> https://www.abc.com?x=3
https://abc.com/login?x=1 -> https://www.abc.com/login?x=1
http://www.abc.com/login?x=3 -> https://www.abc.com/login 

我的 abc.com.conf vhost 文件中有以下内容,但它无法正常工作

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

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

RewriteCond %{HTTPS} off
RewriteRule (.*) https://www.abc.com/%{REQUEST_URI}

</VirtualHost>

<VirtualHost  *:443>
ServerName abc.com
ServerAlias www.abc.com        

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


 </VirtualHost>

有人能帮助我解决剩下的问题吗?

[已编辑以澄清]

答案1

首先,您似乎想要强制所有人使用 SSL。我会首先在您的 *:80 块中处理这个问题:

<VirtualHost *:80>

  ServerName abc.com
  ServerAlias www.abc.com        

  Redirect permanent / https://www.abc.com/

</VirtualHost>

然后执行 *:443 .. 但是您可以使其变得非常简单,只需将 abc.com 拉到单独的虚拟主机中即可:

NameVirtualHost *:80
NameVirtualHost *:443

<VirtualHost *:80>
  ServerName abc.com
  Redirect permanent / https://www.abc.com/
</VirtualHost>

<VirtualHost *:443>
  ServerName abc.com
  Redirect permanent / https://www.abc.com/
   ....
    ...
   # Note you need a wildcard cert.. I assue you have one already or are using self signed. 
</VirtualHost>

好的,现在您已将所有 abc.com 重定向到 www.abc.com,并且您可以照常设置此虚拟主机,而无需进行所有重写。

<VirtualHost *:80>

  ServerName www.abc.com        
  Redirect permanent / https://www.abc.com/
</VirtualHost>

<VirtualHost *:443>
  ServerName www.abc.com
  ...
   ....
</VirtualHost>

相关内容