12

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.

user9517
  • 115,471
  • 20
  • 215
  • 297
Victor S
  • 243
  • 1
  • 2
  • 7

1 Answers1

15

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>
Yohan W. Dunon
  • 163
  • 1
  • 1
  • 10
user9517
  • 115,471
  • 20
  • 215
  • 297
  • 1
    Yes, you are right, but I am puzzled about how to do this on a default Ubuntu 10.4 stack, where the whole shenanigans is managed by having a sites-available and sites-enabled directories, and each virtual host definition exists in its own exclusive file... how do I control the order in that case? – Victor S Nov 20 '11 at 02:10
  • The files are loaded in alphabetic order (the default sort order for ls). – user9517 Nov 20 '11 at 08:22
  • 1
    for a site definition that needs to come first, name it something like `000-excluded.example.com.conf` in the sites-available dir. – Brian Minton Aug 15 '14 at 13:55
  • I was looking to do exactly the same thing, and this does not work for me. The excluded.example.com VirtualHost which had "ServerName excluded.example.com\n Redirect / https://example.com/excluded" caused all requests to the more general VirtualHost to get redirected (which definitely followed the specific VirtualHost in the config). – quuxman Aug 03 '16 at 13:35