1

I'm investigating the configuration of Name-based virtual hosts, under apache, for the provisioning of a main domain, and its sub-domain. I've found one example on the web that suggests a configuration as follows:

Listen 80
NameVirtualHost *

<VirtualHost *>
ServerName www.domain.com
DocumentRoot /home/httpd/htdocs/
</VirtualHost>

<VirtualHost *>
ServerName subdomain.domain.com
DocumentRoot /home/httpd/htdocs/subdomain/
</VirtualHost>

which makes sense until the DocumentRoot directives. Am I correct in saying that it's less than wise to have one virtual hosts's document root, a descendant of another? Doesn't that configuration mean that the "subdomain" directory will server up content at both domains?

1
  • My hosting provider does this and I think it's dumb. Subdomains should be completely separate.
    – Brian
    Jan 18 '11 at 15:13
3

This example will work. However, the content at http://www.example.com/subdomain/ will be the same as http://subdomain.example.com/

What I would use is:

<VirtualHost *>
ServerName www.example.com
DocumentRoot /home/httpd/www.example.com/htdocs/
</VirtualHost>

<VirtualHost *>
ServerName subdomain.example.com
DocumentRoot /home/httpd/subdomain.example.com/htdocs/
</VirtualHost>
1
  • Thanks. That was my suspicion - I guess it's just a misleading example.
    – Bobby Jack
    Jan 18 '11 at 14:53
0

Sometimes it is advantageous to be able to access domain.com/subdomain until the dns for the new subdomain becomes active. This example configuration is the default with many control panels, and unless you have specific needs otherwise, it doesn't actually hurt anything.

You can always add a .htaccess mod_rewrite config that confirms that the http_host is subdomain.domain.com and if not, do a redirect, if you don't want users to be able to access in that fashion. (this is really your only choice if you are not the server administrator, and the control panel lays out your docroot in this fashion)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.