Santiago
Santiago

Reputation: 21

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>

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

Upvotes: 2

Views: 925

Answers (1)

digijay
digijay

Reputation: 1366

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.

Upvotes: 2

Related Questions