简体   繁体   中英

Apache Vhosts HTTP and HTTPS (both) redirect to another domain

I am trying to redirect both versions of Domain-A (HTTP and HTTPs) to another domain Domain-B. Pretty simple stuff but not that simple.

See what i am currently doing now:

<VirtualHost *:80>
    ServerName DOMAIN-A.com
    ServerAlias www.DOMAIN-A.com
    RewriteEngine on
    RewriteRule (.*) http://DOMAIN-B.com? [R=301,L]
</VirtualHost>
<VirtualHost *:443>
    ServerName DOMAIN-A.com
    ServerAlias www.DOMAIN-A.com
    RewriteEngine on
    RewriteRule (.*) https://DOMAIN-B.com? [R=301,L]
</VirtualHost>
<VirtualHost *:443>
    ServerName DOMAIN-B.com
    DocumentRoot /var/www/DOMAIN-B/html
    SSLEngine on
    SSLCertificateFile "/etc/letsencrypt/live/DOMAIN-B/cert.pem"
    SSLCertificateKeyFile "/etc/letsencrypt/live/DOMAIN-B/privkey.pem"
    SSLCertificateChainFile "/etc/letsencrypt/live/DOMAIN-B/chain.pem"
</VirtualHost>
  • Requests to HTTP (Port 80) DOMAIN-A to DOMAIN-B as expected

  • Requests to HTTPS (Port 443) DOMAIN-A do not redirect, instead gives me SSL certificate error. As DOMAIN-A does not have a SSL is presenting my default server certificate instead of redirecting. (SSL BAD DOMAIN) When I accept the exception in browser then it redirects.

Question ***

Can you see any error on my vhost file? or do I need to create an SSL certificate for a domain I am redirecting? any easier workarounds?

Many thanks

You have to include the SSL files for DOMAIN-A.com on port 443 like this:

<VirtualHost *:80>
    ServerName DOMAIN-A.com
    ServerAlias www.DOMAIN-A.com
    Redirect permanent / https://DOMAIN-B.com/
</VirtualHost>
<VirtualHost *:443>
    ServerName DOMAIN-A.com
    ServerAlias www.DOMAIN-A.com
    SSLEngine on
    SSLCertificateFile /etc/letsencrypt/live/DOMAIN-A/cert.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/DOMAIN-A/privkey.pem
    SSLCertificateChainFile /etc/letsencrypt/live/DOMAIN-A/chain.pem
    Redirect permanent / https://DOMAIN-B.com/
</VirtualHost>
<VirtualHost *:443>
    ServerName DOMAIN-B.com
    DocumentRoot /var/www/DOMAIN-B/html
    SSLEngine on
    SSLCertificateFile /etc/letsencrypt/live/DOMAIN-B/cert.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/DOMAIN-B/privkey.pem
    SSLCertificateChainFile /etc/letsencrypt/live/DOMAIN-B/chain.pem
</VirtualHost>

Also you had a little error in DOMAIN-A.com which redirected to http://DONMAIN-B.com/ but there is no configuration for that domain on port 80. Maybe you should add this too.

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