Apache – Using Same Directory Directive for Multiple Virtual Hosts

apache-2.2virtualhost

Here's a sample VirtualHost entry

<VirtualHost *:80>
  ServerName domain.com
  ErrorLog logs/domain.com-error_log
  CustomLog logs/domain.com-access_log common
  DocumentRoot "/var/www/srs/web"
  DirectoryIndex index.php
  Alias /sf /usr/share/pear/data/symfony/web/sf
  <Directory "/usr/share/pear/data/symfony/web/sf">
    AllowOverride All
    Allow from All
  </Directory>
  <Directory "/var/www/srs/web">
    AllowOverride All
    Allow from All
  </Directory>
</VirtualHost>

Now, I have 6 other domains to setup this way. They will all share a single DcoumentRoot, so the only thing I need to setup that's unique per domain is the ServerName and *Log values. Since I'm mostly a copy-paster when it comes to this kind of stuff, what I'm prepared to do is just copy this block 6 times and change the bits I need to.

So, can I at least pull out the Directory entries and globalize them somehow?

Best Answer

Pull your Directory stanzas outside of your VirtualHost containers and it should do what you want.

<VirtualHost *:80>
  ServerName domain.com
  ErrorLog logs/domain.com-error_log
  CustomLog logs/domain.com-access_log common
  DocumentRoot "/var/www/srs/web"
  DirectoryIndex index.php
  Alias /sf /usr/share/pear/data/symfony/web/sf
</VirtualHost>

<VirtualHost *:80>
  ServerName domain2.com
  ErrorLog logs/domain2.com-error_log
  CustomLog logs/domain2.com-access_log common
  DocumentRoot "/var/www/srs/web"
  DirectoryIndex index.php
  Alias /sf /usr/share/pear/data/symfony/web/sf
</VirtualHost>

<Directory "/usr/share/pear/data/symfony/web/sf">
  AllowOverride All
  Allow from All
</Directory>
<Directory "/var/www/srs/web">
  AllowOverride All
  Allow from All
</Directory>
Related Topic