HomeApache Page 4 - Custom Error Pages with Apache
Maintaining and updating error pages - Apache
Nobody enjoys seeing an error page. It's worse if the error page gives you little or no information to help you find the page you were trying to reach. Fortunately, there are ways to configure Apache so that your visitors need never feel so abandoned. This article explains how to set up custom error pages with Apache.
You are now faced with the same problem that we initially set out to fix: bland and generally unhelpful error response documents. However, the fact that the server-generated page makes use of included html pages means that you have a centralized method of maintaining and updating these error pages to fit in with your site's design, making them much easier to customize and manage. If you look at one of the include pages in a text editor, you’ll see a special section there that defines some very basic styles. This is where you can add your own style rules in order to customize the look of the error page.
The 404 response is probably going to be the most common error result that is going to be seen by visitors to your site. Sometimes, this is going to be because they’ve misspelled a URL or clicked on a link to a resource that no longer exists, but very often, it can be the result of site reorganization, or pages being moved from one directory to another within your site's internal structure. Client redirection is extremely easy to configure; you simply use the Redirect directive, the name of the old directory and the URL of where requests to the old directory are redirected to:
Redirect /olddir/ http://localhost/newdir/
This will only be effective when there are no longer any pages in the olddir directory however, as any request for a page within that directory will be redirected to the newdir, so a request for www.mysite.co.uk/2005/quarter1.html where 2005 is set in the place of olddir will automatically be redirected to www.mysite.co.uk/2006/quarter1.html when http://mysite.co.uk/2006/ is set as the newdir. If just an individual page has been moved, you can simply add a directive specifically for the page that has moved:
You can even use the Redirect directive in conjunction with your customized error pages by making use of the status argument, which comes directly after the directive name. The status can be an error code or one of several keywords. If, for example, a page has been removed from your site rather than just moved, you can add the following directive to your conf file to let visitors know:
Redirect 404 /olddir/deletedpage.html
This will generate the HTTP_GONE.html.var error response when internationalized error messages have been configured.
Apache has many other directives that can be used to deal with ever more complex redirection issues, but these are beyond the scope of this article. You’ve seen how easy it is to configure your own set of custom error response pages, and how to enable customized error responses that can be displayed in a variety of languages depending on the preferences of requesting clients. Additionally, you’ve learned how to redirect browsers when requesting pages that have moved or been removed so you can try to minimize the number of times your custom error pages need to be displayed.