Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm running a web server with Apache2 and several virtual domains. Also, there's a mail server for these domains, exposing a web interface. For each virtual domain, I have the subdomain pointing to my webmail interface (Roundcube). It works nicely with the following configuration:

<VirtualHost *:80>
    ServerAlias,,, #...

    DocumentRoot /srv/www/mail/
    <Directory />
            Options FollowSymLinks
            AllowOverride None
    <Directory /srv/www/mail>
            Options Indexes FollowSymLinks MultiViews
            AllowOverride None
            Order allow,deny
            allow from all

    ErrorLog /var/log/apache2/mail.error.log
    LogLevel warn

    CustomLog /var/log/apache2/mail.access.log combined

However, for each new customer, I have - among other things - to add the corresponding domain name to the list of ServerAliases.

Is there a way to automatically associate all mail.* subdomains with /srv/www/mail/?

share|improve this question
ServerAlias supports wildcards - you should be able to use mail.* as a valid ServerAlias (although, I can't seem to find any evidence showing its use that way, it is commonly used the other way). I have used it in that format before for a number of common subdomains, but my VirtualHost block then contained a rewrite to a common URL - however, it since it was able to successfully capture the subdomain, it should work in your scenario. – cyberx86 Dec 4 '11 at 1:34
cyberx86: Stupid me! What you suggested works perfectly. Post it as an answer, so I can accept it. – Philip Dec 4 '11 at 9:28
up vote 5 down vote accepted

The ServerAlias directive supports wildcards - you should be able to use the following to match all the 'mail' subdomains.

ServerAlias mail.*

The wildcard subdomain (i.e. *.domain.tld) is well documented, but it is hard to find any indication that the domain part can be also be a wildcard.

I have used this approach quite often for common subdomains, when I used to use Apache. The only difference being that I used a redirect to a common URL (because I wanted to use SSL, and didn't want to get a certificate for every domain). None the less, it was able to successfully capture the subdomain, so the same implementation should work in your scenario. (Of course, you still need a static ServerName directive as well).

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.