1

I'm trying to set up a single apache virtualhost file that will route xyz.mycompany.com to /var/www/html/development/xyz/public

Here is my hard-coded version. Is there a way to swap "xyz" with a variable?

<VirtualHost *:80>
    ServerName xyz.mycompany.com
    ServerAlias xyz.mycompany.com
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html/development/xyz/public

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    <Directory /var/www/html/development/xyz>
        AllowOverride All
        Options -Indexes +FollowSymLinks +MultiViews
        Order allow,deny
        Allow from all
    </Directory>
</VirtualHost>
Anthony
  • 111
  • 3
  • 15

2 Answers2

1

mod_macro can do variable substitution in config files. However your subject says "wildcard substitution" and certainly Apache config files can handle *. In your case ServerAlias *.mycompany.com would send any host headers for any subdomain off of mycompany.com into your /var/www/html/development/xyz directory.

Wesley
  • 32,690
  • 9
  • 82
  • 117
  • Thanks Wesley. I think lulian's answer more closely matches what I'm looking for. I'm trying to match the subdomain to the directory. – Anthony Dec 31 '14 at 16:52
1

Please read this one as well: Apache2 dynamic documentroot depending on URL

You can do this:

<VirtualHost *:80>
    ServerName xyz.mycompany.com
    ServerAlias *.mycompany.com
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/html/development/%1/public

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    <Directory /var/www/html/development>
        AllowOverride All
        Options -Indexes +FollowSymLinks +MultiViews
        Order allow,deny
        Allow from all
    </Directory>
</VirtualHost>

Regarding the <Directory> part, I think you can set your options for the entire development directory.

Iulian
  • 428
  • 1
  • 3
  • 8
  • Update: "DocumentRoot" needs to be "VirtualDocumentRoot", and you have to run `a2enmod vhost_alias` to install the VirtualDocumentRoot command beforehand. Finally, run `service apache2 reload` – Anthony Dec 31 '14 at 18:19
  • You are welcome. – Iulian Jan 01 '15 at 18:18