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?


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



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 http://httpd.apache.org/docs/2.0/mod/mod_vhost_alias.html
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/mod_vhost_alias.so
  • 1
    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 '16 at 23:51
  • Make sure to verify that both www.domain.com & domain.com work. May 26 '17 at 9:08

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.

  • 1
    I think #2 will require restarting httpd service though?
    – mr1031011
    Oct 30 '13 at 16:13
  • @James A. Rosen - can you show us how you configured this and in which file?
    – MonTea
    Sep 5 '20 at 15:39

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.