DekGenius.com
[ Team LiB ] Previous Section Next Section

Recipe 4.2 Designating One Name-Based Virtual Host as the Default

Problem

You want all requests, whether they match a virtual host 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 *>
    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 *> notation, and that there is also an accompanying NameVirtualHost * 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

    [ Team LiB ] Previous Section Next Section