Usually 404 pages are handled within your framework or CMS, but some cases call for an htaccess 404 page. This article explains how to create 404 functionality using htaccess.

Create a 404 page

Before you can display a 404 page, you need to create one to display. If you use PHP you can grab stats about what pages are 404ing and where the user is visiting these pages from.

Modify your htaccess file

If you have been reading my other Apache articles, by now you will be familiar with editing the .htaccess file. Add the following line to your htaccess file:

ErrorDocument 404 /404.php

I like to place my 404 page in the root directory of the web accessible files, others prefer an errors directory. The errors directory is probably more correct if you have multiple error files.

That is all it takes. I'd like to stress that if you are doing any web development 99% of the time you should be using a CMS or a framework, which will already have solutions for error pages.

Other Errors

If you would like to set custom pages for other error codes the setup is the same with a different code:

ErrorDocument 400 /errors/400badrequest.html
ErrorDocument 401 /errors/401authreqd.html
ErrorDocument 403 /errors/403forbid.html
ErrorDocument 500 /errors/500server.html