简体   繁体   中英

Apache Rewrite Rule for https subdomain redirect

I have a following Apache Configuration:

<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>

I does redirect for the following URLs:

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

However, it does not work for the following URL:

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

Due to this, the user which enters the last URL receives a prompt that certificate is incorrect (understandable — domains don't match). Is there a way to correct this, so that all redirects go to https://www.example.com ?

Thanks in advance!

You need to create virtual host for subdomain.example.com and implement rewrite rule there which will redirect https://subdomain.example.com to https://www.example.com

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM