I'm using Apache and I have a CNAME DNS record for

sub2.sub1.domain1.com that points to sub1.domain1.com

And I have an A DNS record for

sub1.domain1.com that points to the IP.

Then in my httpd.conf file I have:

<VirtualHost *:80>
        ServerName www.domain1.com
        DocumentRoot /domain1/www
</VirtualHost>

<VirtualHost *:80>
        ServerName sub1.domain.com
        DocumentRoot /domain1/sub1/www
</VirtualHost>

Yet I appear to be missing something, as when you visit sub2.sub1.domain1.com, it shows you the page for domain1.com. When you visit the sub1.domain1.com, it shows the correct page for sub1.


Solution 1:

Though you have CNAME record to point sub2.sub1.domain1.com to the correct server, apache server maybe serving multiple website.

When a http request come in, apache check the URL against ServerName to determine which website to serve out. Should there be any additional name pointing to the same website, you put them in ServerAlias.

In your httpd.conf, you have to add ServerAlias for sub2.sub1.domain1.com

<VirtualHost *:80>
    ServerName sub1.domain.com
    ServerAlias sub2.sub1.domain1.com
    DocumentRoot /domain1/sub1/www
</VirtualHost>

Additionally, the first VirtualHost declaration in httpd.conf is used as the default. When a http request comes in and doesn't match any ServerName and ServerAlias, the default site is serve out.