Using wildcard in ServerName and ServerAlias of Apache virtualhost
There are number of situations when you want to create 1 common subdomain for all hosted domains on your server. For example when you want all projects/domains to have webmail access, like so mail.domain1.com, mail.domain2.com etc.
You could create virtualhost and manually add subdomains in ServerAlias, but that’s too much trouble isn’t it?
Wildcard to the rescue…
Surprisingly, documentation about the use of wildcard in apache2 ServerName and ServerAlias are nonexistent. So I had to go through several tries to get it right.
My initial hunch was to go with ServerName mail.*, apache didn’t complain but mail subdomain didn’t work.
Anyways this is a working virtualhost:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<virtualhost *:80> ServerAdmin sandrodz@ ServerName mail.domain1.com ServerAlias mail.* #DirectoryIndex index.html DocumentRoot /var/www/projects/webmail #Allow .htaccess files to work <Directory /var/www/projects/webmail> Options FollowSymLinks AllowOverride All </Directory> #Custom log file locations LogLevel warn ErrorLog ${APACHE_LOG_DIR}/webmail_error.log CustomLog ${APACHE_LOG_DIR}/webmail_access.log combined </virtualhost> |
Leave a Reply