72

If I have 3 domains, domain1.com, domain2.com, and domain3.com, is it possible to set up a default virtual host to domains not listed? For example, if I would have:

<VirtualHost 192.168.1.2 204.255.176.199>
DocumentRoot /www/docs/domain1
ServerName domain1
ServerAlias host
</VirtualHost>

<VirtualHost 192.168.1.2 204.255.176.199>
DocumentRoot /www/docs/domain2
ServerName domain2
ServerAlias host
</VirtualHost>

<VirtualHost 192.168.1.2 204.255.176.199>
DocumentRoot /www/docs/everythingelse
ServerName *
ServerAlias host
</VirtualHost>

If you register a domain and point it to my server, it would default to everythingelse showing the same as domain3. Is that possible?

SJaguar13
  • 937

10 Answers10

97

When using name-based virtual hosts, the first virtual host configuration loaded will be the default (Source: Apache Wiki). For example, with the configuration below, otherwise unmatched domains will match with domain-one.com:

NameVirtualHost *:80

<VirtualHost *:80>
  ServerName domain-one.com
  # Other options and directives ..
</VirtualHost>

<VirtualHost *:80>
  ServerName domain-two.com
  # Other options and directives ..
</VirtualHost>

Many servers do not have a monolithic configuration file, but have several host-specific configuration files organized as follows:

/etc/apache2
|-- sites_available  (actual configuration files)
`-- sites_enabled    (symlinks to files in sites_available)

In this case, to make a particular virtual host configuration load first, rename the symlink to something which will be first when sorted, such as 00-default.


Some of the other answers are not quite correct. According to the Apache Wiki, not setting a ServerName in a virtual host is incorrect. If the host without a ServerName is not loaded first, Apache may never even use it, since the first host loaded would be the default.

Furthermore, while ServerAlias * will indeed match anything, it may also override other virtual hosts defined later. Maybe this approach would work if it's always the last virtual host to be defined (as in the configuration given in the question), but this means adding a new directive and changing the sort order instead of just changing the order as above.

  • Do you know which one comes first, httpd.conf or conf.d/xyz.conf? – Esa Varemo Sep 16 '12 at 21:59
  • 2
    "the first virtual host configuration loaded will be the default" solved my problem with local SSL domains on XAMPP (Windows). It looks like Apache uses first vhost as defaults for each port, so in order to properly handle non-matched domains for both unsecured/secured requests, there should be 2 explicit "default" configs for 80/443 ports defined at the beginning of httpd-vhosts.conf – Wirone Apr 17 '15 at 07:17
  • 1
    @EsaVaremo - httpd.conf will be loaded first, and it will have an Include line that sources conf.d/xyz.conf (or likely, conf.d/*). any config (including vhosts) before the Include line will be processed first; anything after the include line is processed after the included files. – Dan Pritts May 01 '15 at 15:48
  • Thank you so much for mentioning ServerAlias *. – SuperSandro2000 Jun 14 '21 at 13:18
  • The '00-' prefix did the work, awesome. – Huseyin Yagli Apr 08 '22 at 20:30