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 [email protected]

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

<VirtualHost *:80>
    ServerName beta.example.com
    ServerAdmin [email protected]

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

<VirtualHost *:80>
    ServerName example.com
    ServerAlias *.example.com
    ServerAdmin [email protected]

    DocumentRoot /var/web/www
    <Directory /var/web/www/>
        ...
    </Directory>
...
</VirtualHost>
2
  • 1
    Your config should do just that. What problem are you having? Aug 6, 2013 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. Aug 6, 2013 at 23:17

1 Answer 1

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.
2
  • @shane-madden Are you 100% sure about wildcards being trumphed ? 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, 2016 at 18:45
  • From a quick test, I confirm the order matters. Wildcard should be last.
    – Jérôme
    Oct 22, 2019 at 21:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .