Redirect one vhost to another vhost with SSL in Apache

6,484

I suggest you to use A record over CNAME in DNS settings to simplify the proceedings. Refer Differences between the A, CNAME, ALIAS and URL records

Following vhost config would fulfill your requirement.

1) HTTP domain.one.com to HTTPS domain.two.com

NameVirtualHost *:80
NameVirtualHost *:443

<VirtualHost *:80>

    ServerName domain.one.com
    RewriteEngine On
    RewriteRule ^(.*)$       "https://domain.two.com$1" [R,L]

    ErrorLog logs/error_log
    CustomLog logs/access_log common

    DocumentRoot "/var/www/html/"

    <Directory "/var/www/html/">
        Require all granted
        Options Indexes FollowSymLinks
        AllowOverride All
        Order allow,deny
        Allow from all
    </Directory>

</VirtualHost>

<VirtualHost *:443>

    ServerName domain.two.com
    SSLEngine On
    SSLCertificateFile /etc/apache2/ssl/your.crt
    SSLCertificateKeyFile /etc/apache2/ssl/your.key
    SSLCertificateChainFile /etc/apache2/ssl/chain.crt

    ErrorLog logs/error_log
    CustomLog logs/access_log common

    DocumentRoot "/var/www/html/"

    <Directory "/var/www/html/">
        Require all granted
        Options Indexes FollowSymLinks
        AllowOverride All
        Order allow,deny
        Allow from all
    </Directory>

</VirtualHost>

2) For both HTTP and HTTPS domain.one.com to HTTPS domain.two.com

NameVirtualHost *:80
NameVirtualHost *:443

<VirtualHost *:80>

    ServerName domain.one.com
    RewriteEngine On
    RewriteRule ^(.*)$       "https://domain.two.com$1" [R,L]

    ErrorLog logs/error_log
    CustomLog logs/access_log common

    DocumentRoot "/var/www/html/"

    <Directory "/var/www/html/">
        Require all granted
        Options Indexes FollowSymLinks
        AllowOverride All
        Order allow,deny
        Allow from all
    </Directory>

</VirtualHost>

<VirtualHost *:443>

    ServerName domain.one.com
    RewriteEngine On
    RewriteRule ^(.*)$       "https://domain.two.com$1" [R,L]

    SSLEngine On
    SSLCertificateFile /etc/apache2/ssl/your.crt
    SSLCertificateKeyFile /etc/apache2/ssl/your.key
    SSLCertificateChainFile /etc/apache2/ssl/chain.crt

    ErrorLog logs/error_log
    CustomLog logs/access_log common

    DocumentRoot "/var/www/html/"

    <Directory "/var/www/html/">
        Require all granted
        Options Indexes FollowSymLinks
        AllowOverride All
        Order allow,deny
        Allow from all
    </Directory>

</VirtualHost>


<VirtualHost *:443>

    ServerName domain.two.com
    SSLEngine On
    SSLCertificateFile /etc/apache2/ssl/your.crt
    SSLCertificateKeyFile /etc/apache2/ssl/your.key
    SSLCertificateChainFile /etc/apache2/ssl/chain.crt

    ErrorLog logs/error_log
    CustomLog logs/access_log common

    DocumentRoot "/var/www/html/"

    <Directory "/var/www/html/">
        Require all granted
        Options Indexes FollowSymLinks
        AllowOverride All
        Order allow,deny
        Allow from all
    </Directory>

</VirtualHost>
Share:
6,484

Related videos on Youtube

Alex
Author by

Alex

Updated on September 18, 2022

Comments

  • Alex
    Alex over 1 year

    Here is my situation: I have two CNAME domains (domain.one.com and domain.two.com) that point to the same IP/machine and I want to redirect one of the two to the other one, like domain.one.com/anything to domain.two.com/anything. domain.two.com has a valid SSL certificate, if it matters.

    And I have the following settings:

    In httpd.conf:

    RewriteCond %{HTTP_HOST}   ^domain\.two\.com [NC]
    RewriteCond %{HTTP_HOST}   !^$
    RewriteRule ^/(.*)         https://domain.two.com/$1 [L,R]
    
    NameVirtualHost *:80
    NameVirtualHost domain.two.com:443
    
    <VirtualHost *:80>
        ServerName domain.two.com
        DocumentRoot "/var/www/html/"
        ErrorLog logs/error_log
        CustomLog logs/access_log common
        RewriteEngine On
        RewriteOptions Inherit
    </VirtualHost>
    

    In ssl.conf:

    <VirtualHost domain.two.com:443>
        DocumentRoot "/var/www/html"
        ServerName domain.two.com:443
    </VirtualHost>
    

    --

    What I have tried so far:

    Adding to httpd.conf the following:

    NameVirtualHost domain.one.com:443
    
    <VirtualHost domain.one.com:80>
        ServerName domain.one.com
        RewriteEngine On
        RewriteCond %{HTTP_HOST} ^domain.one.com$
        RewriteRule (.*) http://domain.two.com/$1 [R=Permanent]
    </VirtualHost>
    
    • Doesn’t redirect.

    Also adding the following to ssl.conf:

    <VirtualHost domain.one.com:443>
        ServerName domain.one.com
        RewriteEngine On
        RewriteCond %{HTTP_HOST} ^domain.one.com$
        RewriteRule (.*) http://domain.two.com/$1 [R=Permanent]
    </VirtualHost>
    
    • The server stops working.

    I know I’m doing something wrong, but I don’t know what. How can I make this work?

    (I should mention that I’m a noob in these things.)

  • Alex
    Alex about 8 years
    Where should I put these settings, in httpd.conf or in ssl.conf? As now I have them divided between the two.
  • Rumesh Bandara
    Rumesh Bandara about 8 years
    I suggest you to keep a single vhost file and all the config in same. In Ubuntu, you can do that easily. BTW what is the OS that you use?
  • Rumesh Bandara
    Rumesh Bandara about 8 years
    Rather than adding vhost config in httpd.conf, you can add separate vhosts in CentOS. Please refer Step four (Create New Virtual Host Files) in How To Set Up Apache Virtual Hosts on CentOS 7