Exclude specific domains from Apache2 serverAlias while using a catch all *(wildcard) alias

apache-2.2serveraliaswildcard

I have a web application that needs to support custom domains, in that regard I have set-up the following name based virtual server:

<VirtualHost *:80>
    ServerName example.com
    ServerAlias * *.example.com www.example.com example.com
    RailsEnv production
    RackEnv production
    DocumentRoot /srv/www/example/current/public
    <Directory /srv/www/example/current/public>
             AllowOverride all
             Options -MultiViews FollowSymLinks
    </Directory>
    ErrorLog /srv/www/example/log/error.log
    TransferLog /srv/www/example/log/access.log
</VirtualHost>

Notice the * as the server alias? that catches all the domains on that server. However, I have other sites on this server which I want to be excluded from this list. It is more economical for me to have a list of excluded domains than manually set every domain a user may register with at this service as a serverAlias…

Perhaps this is not the best way to go, but I'm looking for help, in the best (relatively simple) way to set up a web-app that may catch any domains, while allowing other specific domains to be routed to different apps.

Best Answer

Apache searches for a match in the order that the domains are defined.

If I understand your problem correctly then it can be solved by defining your hosts to be excluded before the catch all host.

Example:

<VirtualHost *:80>
    ServerName excluded.example.com
    ServerAlias  something.example.com ...
    ...
</VirtualHost>
<VirtualHost *:80>
    ServerName example.com
    ServerAlias * *.example.com www.example.com example.com
    RailsEnv production
    ...
</VirtualHost>
Related Topic