0

Is there a way to setup a catchall (for subdomains) virtual server for a single domain, where I have a couple pre-defined subdomains.

ie:

webmail.example.com -> points to "/var/web/roundcube"

beta.example.com -> points to "/var/web/beta/"

example.com and all other subdomains -> point to "/var/web/www"

<VirtualHost *:80>
    ServerName webmail.example.com
    ServerAdmin webmaster@example.com

    DocumentRoot /var/web/roundcube
    <Directory /var/web/roundcube/>
        ...
    </Directory>
...
</VirtualHost>

<VirtualHost *:80>
    ServerName beta.example.com
    ServerAdmin webmaster@example.com

    DocumentRoot /var/web/beta
    <Directory /var/web/beta/>
        ...
    </Directory>
...
</VirtualHost>

<VirtualHost *:80>
    ServerName example.com
    ServerAlias *.example.com
    ServerAdmin webmaster@example.com

    DocumentRoot /var/web/www
    <Directory /var/web/www/>
        ...
    </Directory>
...
</VirtualHost>
Aaron Murray
  • 139
  • 2
  • 8
  • 1
    Your config should do just that. What problem are you having? – Shane Madden Aug 06 '13 at 23:13
  • My question is, is this the proper way to do it, and in what order does it get loaded (if say each are in separate config files), does the wildcard take precedence if it is loaded first? Or is there a better / more appropriate way to do the above? Sorry I didn't make that very clear when I asked the question. – Aaron Murray Aug 06 '13 at 23:17

1 Answers1

2

That should work just fine, and this is definitely the proper way to do what you're trying to do!

  • Load order doesn't matter in your example, since these virtual hosts cover all the domains you're serving content for.
    • An exact match to a ServerName or ServerAlias will trump a wildcard ServerAlias, regardless of the order they were loaded in.
    • The load order matters for requests which don't match any of the ServerName or ServerAlias directives in any way (say, with the IP in the URL instead of hostname) - those requests will be served by the first virtual host to load.
Shane Madden
  • 114,520
  • 13
  • 181
  • 251
  • @shane-madden Are you 100% sure about wildcards being trumphed ? https://httpd.apache.org/docs/current/mod/core.html#serveralias suggests otherwise "The **first matching** ServerName or ServerAlias is used, with no different precedence for wildcards" – Lazy404 May 31 '16 at 18:45
  • From a quick test, I confirm the order matters. Wildcard should be last. – Jérôme Oct 22 '19 at 21:35