6

My httpd-vhosts.conf file looks like this:

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

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

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

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?

1

3 Answers 3

3

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

http://httpd.apache.org/docs/2.0/vhosts/mass.html

2

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
2
  • 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, 2016 at 23:51
  • Make sure to verify that both www.domain.com & domain.com work.
    – Andy Swift
    May 26, 2017 at 9:08
0

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.

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

You must log in to answer this question.

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