Creating Custom Error Pages with .htaccess

Please note: this article is over 16 years old. Information may be out of date.

Have you ever noticed that some people have really nice error pages when a page doesn't exist on their site? Wouldn't it be nice if you could make your error page match the rest of your site? Don't fret; you can do it! All you'll need to do is use a handly little file called .htaccess.

Web Server

First thing you need to know is that .htaccess is a very powerful file that controls a lot of important parts of your site. Before you change anything, make sure to make a backup copy. If anything goes wrong, you can revert to the backup. If there isn't already an .htaccess file on your server, you can create one in Windows Notepad or your favorite HTML editor. If you're using Windows, make sure you save the file as ".htaccess" including the quote marks.

Next, you need to little bit about HTTP response codes. What are those, you ask? Well, any time you access a page from your browser, the server that contains the page (or that you think contains the page) looks for the page and sends a numeric code back to your browser. If everything goes alright, the server sends the code 200 back to you and the page is displayed. If the page isn't found, the server sends a 404 code back. That's why page-not-found errors are commonly called 404 errors. There are lots of different HTTP codes, but you'll only need to make pages for a few of them:

  • 400: Bad request.
    The server can't understand the request you're sending. This is rarely seen (hopefully).
  • 401: Authorization required.
    In order to access this page, you'll need a username and password.
  • 403: Forbidden.
    The server was instructed to refuse your request.
  • 404: Page not found.
    Our friend the 404 error. You'll use this page the most often.
  • 408: Timeout.
    The request could not be completed in the time allowed for it. Your browser will usually automatically generate a message when this occurrs.
  • 500: Internal server error.
    Something is misconfigured in the server. This is generally caused by problems with scripts or your .htaccess file.

Okay, now that you understand a little bit about what .htaccess can do, we're ready to start doing the grunt work. The first thing we're going to need to do is make our error page. You can either make a different page for each error or just have one standard error page. The choice is yours, but we're going to make a 404 error page in this tutorial. Go ahead and put some HTML on your page, and feel free to spice it up a little bit. In fact, do spice it up. Use the stylesheet that matches your site. Include a link to your home page. Post your email address so people can contact you if there's a problem. (If you're worried about spam bots, there are ways to obfuscate your email address.) Once your done, save your page as an HTML file. We're going to call ours error404.html and save it in the /errors directory.

Once you're done with that, it's time to do the magic. Open up the .htaccess file that you found/created earlier. If yours is premade, find some blank space at the bottom. Now, insert the following line of code:

ErrorDocument 404 /errors/error404.html

That's it! If you want to add more error documents, just add more ErrorDocument lines. The general format for the line is

ErrorDocument HTTP_error_number /path/to/errorpage.ext

If you want to be really fancy, you can use appropriately named CGI, PHP, ASP or other pages as your error page. Now, go type some random URLs to check your brand-spanking new error pages!

Want to learn more about .htaccess? Visit

Improve Your Display Campaigns & Get More ROI

Rate This Article

How would you rate the quality of this content?
Currently rated: 2.2 out of 5 stars. 4 users have rated this article. Select your rating:
  • 2.2 out of 5 Stars
  • 1
  • 2
  • 3
  • 4
  • 5

About The Author

Eric Reif has seven years of experience with HTML and web design. He has recently begun to work increasingly more with CSS, JavaScript, Java, PHP, and mySQL and still has time to apply to college.

Related Articles

Web Server

How to Use Mod_rewrite For URL Rewriting in Apache

URL Rewriting is the process of manipulating an URL or a link, which is send to a web server in such a way that the link is dynamically modified at the server to include additional parameters and information along with a server initiated redirection.
Restricted Area

How to Restrict Access to a Directory Using Password Authentication

This article explains how to restrict access to directories with Basic HTTP Authentication. Basic HTTP Authentication (or Basic Access Authentication) is a simple security mechanism to restrict access to websites or some parts of them by requiring an username and password, which is managed by a visitor's web browser when making a request.
Web Server

Are You Ready for High Volume Traffic?

Many webmasters wish they got a lot of traffic to their site. A day may come and their website may be very popular in just one day. This could be a reason that unique article is published on their site, or just a simple change in search position results in major search engines.
Web Server

Mod_Rewrite For Newbies

This article is not a complete guide to Apache's mod_rewrite neither to .htaccess. Its purpose is to help you - the webmaster - to create "mod_rewritten" versions of your dynamic webpages even if you have limited technical knowledge.
Web Server

Apache htaccess for PHP web application deployment

The following is a brief introduction to Apache's htaccess file for web application deployment, distribution, or implementation on shared hosting environments. The Apache htaccess file is not new; however it seems to only be used by more advanced Apache users and web application developers.
Get free access to the largest backlink database