Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

My httpd-vhosts.conf file looks like this:

<VirtualHost *:80>
DocumentRoot "/www/foo/htdocs"
ServerName foo

<VirtualHost *:80>
DocumentRoot "/www/bar/htdocs"
ServerName bar

<VirtualHost *:80>
DocumentRoot "/www/baz/htdocs"
ServerName baz

Notice the pattern? Is there any way to cut down on the duplication, or am I stuck with having a separate VirtualHost entry for each site?

share|improve this question
duplicate of… – vartec May 12 '09 at 10:41

I suggest you have a look at the Apache website for dynamic virtual hosts. The other methods require a restart of the Apache server.

share|improve this answer
up vote 2 down vote accepted

I figured out how to do it after reading Dark Ninja's answer. Here's what my httpd-vhosts.conf file looks like now.

# Dynamic virtual hosts.
# See
UseCanonicalName Off
VirtualDocumentRoot /www/%0/htdocs/

I also had to enable mod_vhost_alias by uncommenting this line in httpd.conf

LoadModule vhost_alias_module modules/
share|improve this answer
Just be aware that using VirtualDocumentRoot actually impacts mod_rewrite rules (as used in WordPress, Drupal and many PHP frameworks). It requires you to apply a RewriteBase rule to each app. I'm still trying to figure out a workaround to that. – Simon East Jun 27 at 23:51

There are two very different answers.

  1. Use the "subdomain-as-account-folder"trick.
  2. Write a short script and configuration file that together generate your httpd-vhosts.conf file(s) dynamically. Then the script and configuration become your masters, and the .conf files are "expendable."

I recommend #2, and I think the Pragmatic Programmers might as well.

share|improve this answer
I think #2 will require restarting httpd service though? – mr1031011 Oct 30 '13 at 16:13

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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