0

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

1 Answer 1

1

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>
3
  • Where should I put these settings, in httpd.conf or in ssl.conf? As now I have them divided between the two.
    – Alex
    Apr 12, 2016 at 10:50
  • 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? Apr 12, 2016 at 10:56
  • 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 Apr 12, 2016 at 11:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .