4.2. Designating One Name-Based Virtual Host as the Default
Problem
You want all unmatched requests, whether they specify a name or use an IP address, to be directed to a default host, possibly with a “host not found” error message.
Solution
Add the following <VirtualHost> section, and list it before all of your other ones:
<VirtualHost *:80>
ServerName default
DocumentRoot /www/htdocs
ErrorDocument 404 /site_list.html
</VirtualHost>Discussion
Note that this recipe is used in the context of name-based
virtual hosts, so it is assumed that you have other virtual hosts that
are also using the <VirtualHost
*:80> notation, and that there
is also an accompanying NameVirtualHost *:80 appearing above them. We have used the
default name for clarity; you can
call it whatever you want.
Setting the ErrorDocument 404 to a list of the available sites on the server directs the user to useful content, rather than leaving him stranded with an unhelpful 404 error message. You may wish to set DirectoryIndex to the site list as well, so that users who go directly to the front page of this site also get useful information.
It’s a good idea to list explicitly all valid hostnames either as ServerNames or ServerAliases, so that nobody ever winds up at the default site. However, if someone accesses the site directly by IP address, or if a hostname is added to the address in question before the appropriate virtual host is created, the user still gets useful content.
See Also
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access