Previous Section  < Day Day Up >  Next Section

Recipe 22.16. Making Custom Error Pages

22.16.1 Problem

When a visitor to your web site hits a bad link, you want her to see a nice, customized error page with the same look as the rest of your site, instead of the default stern, cold Apache 404 page:

Not Found
The requested URL /foo was not found on this server.
Apache/2.0.50 (Unix) Server at http.bratgrrl.com Port 80

In fact, you would like your error pages to mirror the look of your web site and be friendly and informative.

22.16.2 Solution

Create your custom page (in this example 404-custom.html), then set the ErrorDocument directive in httpd.conf to point to this page:

ErrorDocument 404 /error/404-custom.html

The /error directory is the default location for error pages.

22.16.3 Discussion

Don't change the default error pages that come with Apache. (Look in your DocumentRoot directory to see the default error pages. In the examples in this chapter, that is /var/www/error.) You can customize these pages a bit, as discussed in the next recipe, but for a major overhaul it is better to create new ones, because the default pages are internationalized. Even if you don't want to use them now, you might as well save them just in case. Apache can use these internationalized pages to automagically deliver error messages in the language of your site visitors via Content Negotiation (see Recipe Recipe 22.19).

To use the internationalized default error pages, open httpd.conf and find the following lines:

# The internationalized error documents require mod_alias, mod_include

# and mod_negotiation.  To activate them, uncomment the following 30 lines.

Then do what they say.

Instead of serving up an error page, you may specify a simple message in httpd.conf:

ErrorDocument 403 "Nobody here, you go away now"

22.16.4 See Also

  • http://localhost/manual/mod/core.html.en#errordocument

    Previous Section  < Day Day Up >  Next Section