0

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.

1 Answer 1

5

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.

6
  • I was under the impression that when utilizing a CNAME record though, I wouldn't have to change any settings in apache.
    – mrc0der
    Nov 10, 2012 at 3:08
  • Updated my answer to clarify.
    – John Siu
    Nov 10, 2012 at 3:15
  • So will I be able to add 10,000 server alias's then? Google Apps lets you create CNAMES. For example mail.domain1.com that all point to ghs.googlehosted.com; Essentially I'm going to be offering a service that will need to allow clients to setup CNAMEs and I will need to handle those requests.
    – mrc0der
    Nov 10, 2012 at 3:21
  • So then, I dont need to do utilize ServerAlias, and just use the default VirtualHost declaration.
    – mrc0der
    Nov 10, 2012 at 3:24
  • @mrc0der If they are going to the same website, you add them to ServerAlias of the corresponding VirtualHost section. If they are seperate website, you create new VirtualHost section for each of them.
    – John Siu
    Nov 10, 2012 at 3:25

You must log in to answer this question.

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