用于https子域重定向的Apache重写规则

时间:2016-06-16 10:17:02

标签: apache mod-rewrite ssl https

我有以下Apache配置:

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

<VirtualHost *:443>
    <IfModule mod_rewrite.c>
            RewriteEngine On

            RewriteCond %{HTTP_HOST}   !^www\.example\.com [NC]
            RewriteRule ^/(.*)         https://www.example.com/$1 [R=301,L]
    </IfModule>
</VirtualHost>

我确实重定向了以下网址:

http://subdomain.example.com -> https://www.example.com
http://example.com -> https://www.example.com
https://example.com -> https://www.example.com

但是,它不适用于以下网址:

https://subdomain.example.com -> https://subdomain.example.com (instead of https://www.example.com)

因此,输入最后一个URL的用户会收到证书不正确的提示(可理解 - 域名不匹配)。有没有办法纠正这个问题,以便所有重定向都转到https://www.example.com

提前致谢!

1 个答案:

答案 0 :(得分:0)

您需要为subdomain.example.com创建虚拟主机,并在那里实施重写规则,将https://subdomain.example.com重定向到https://www.example.com

    <VirtualHost *:443>
   ServerName subdomain.example.com
   RewriteEngine On
      RedirectPermanent / https://www.example.com/
     </VirtualHost>