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 *>
ServerName subdomain.domain.com
DocumentRoot /home/httpd/htdocs/subdomain/

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?

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

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

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.