In Apache, you can set up each directory on your server individually, giving them different properties or requirements for access. And while you can do this through normal Apache configuration, some hosts may wish to give users the ability to set up their own virtual server how they like. And so we have .htaccess files, a way to set Apache directives on a directory by directory basis without the need for direct server access, and without being able to affect other directories on the same server.
One up-side of this (amongst many) is that with a few short lines in an .htaccess file, you can tell your server that, for example, when a user asks for a page that doesn't exist, they are shown a customized error page instead of the bog-standard error page they've seen a million times before. If you visit http://www.addedbytes.com/random_made_up_address then you'll see this in action - instead of your browser's default error page, you see an error page sent by my server to you, telling you that the page you asked for doesn't exist.
This has a fair few uses. For example, my 404 (page not found) error page also sends me an email whenever somebody ends up there, telling me which page they were trying to find, and where they came from to find it - hopefully, this will help me to fix broken links without needing to trawl through mind-numbing error logs.
[Aside: If you set up your custom error page to email you whenever a page isn't found, remember that "/favicon.ico" requests failing doesn't mean that a page is missing. Internet Explorer 5 assumes everyone has a "favicon" and so asks the server for it. It's best to filter error messages about missing "/favicon.ico" files from your error logging, if you plan to do any.]
Setting up your htaccess file is a piece of cake. First things first, open notepad (or better yet, [url=http://www.editplus.com/]EditPlus2[/url]), and add the following to a new document:
ErrorDocument 404 /404.html
Next you need to save the file. You need to save it as ".htaccess". Not ".htaccess.txt", or "mysite.htaccess" - just ".htaccess". I know it sounds strange, but that is what these files are - just .htaccess files. Nothing else. Happy? If not, take a look at this [url=http://wsabstract.com/howto/htaccess.shtml].htaccess guide[/url], which also explains the naming convention of .htaccess in a little more depth. If you do use Notepad, you may need to rename the file after saving it, and you can do this before or after uploading the file to your server.
Now, create a page called 404.html, containing whatever you want a visitor to your site to see when they try to visit a page that doesn't exist. Now, upload both to your website, and type in a random, made-up address. You should, with any luck, see your custom error page instead of the traditional "Page Not Found" error message. If you do not see that, then there is a good chance your server does not support .htaccess, or it has been disabled. I suggest the next thing you do is check quickly with your server administrator that you are allowed to use .htaccess to serve custom error pages.
If all went well, and you are now viewing a custom 404 (page not found) error page, then you are well on your way to a complete set of error documents to match your web site. There are more errors out there, you know, not just missing pages. Of course, you can also use PHP, ASP or CFML pages as error documents - very useful for keeping track of errors.
You can customize these directives a great deal. For example, you can add directives for any of the status codes below, to show custom pages for any error the server may report. You can also, if you want, specify a full URL instead of a relative one. And if you are truly adventurous, you could even use pure HTML in the .htaccess file to be displayed in case of an error, as below. Note that if you want to use HTML, you must start the HTML with a quotation mark, however you should not put one at the other end of the HTML (you can include quotation marks within the HTML itself as normal).
ErrorDocument 404 "Ooops, that page was <b>not found</b>. Please try a different one or <a href="mailto:owner@site.com">email the site owner</a> for assistance.
Server response codes
A server reponse code is a three digit number sent by a server to a user in response to a request for a web page or document. They tell the user whether the request can be completed, or if the server needs more information, or if the server cannot complete the request. Usually, these codes are sent 'silently' - so you never see them, as a user - however, there are some common ones that you may wish to set up error pages for, and they are listed below. Most people will only ever need to set up error pages for server codes 400, 401, 403, 404 and 500, and you would be wise to always have an error document for 404 errors at the very least.
It is also relatively important to ensure that any error page is over 512 bytes in size. Internet Explorer 5, when sent an error page of less than 512 bytes, will display its own default error document instead of your one. Feel free to use padding if this is an issue - personally, I'm not going to increase the size of a page because Internet Explorer 5 doesn't behave well.
In order to set up an error page for any other error codes, you simply add more lines to your .htaccess file. If you wanted to have error pages for the above five errors, your .htaccess file might look something like this:
ErrorDocument 400 /400.html
ErrorDocument 401 /401.html
ErrorDocument 403 /403.html
ErrorDocument 404 /404.html
ErrorDocument 500 /500.html
Informational
- 100 - Continue
- 101 - Switching Protocols
- 200 - OK
- 201 - Created
- 202 - Accepted
- 203 - Non-Authoritative Information
- 204 - No Content
- 205 - Reset Content
- 206 - Partial Content
- 300 - Multiple Choices
- 301 - Moved Permanently
- 302 - Found
- 303 - See Other
- 304 - Not Modified
- 305 - Use Proxy
- 307 - Temporary Redirect
- 400 - Bad Request
- 401 - Unauthorized
- 402 - Payment Required
- 403 - Forbidden
- 404 - Not Found
- 405 - Method Not Allowed
- 406 - Not Acceptable
- 407 - Proxy Authentication Required
- 408 - Request Timeout
- 409 - Conflict
- 410 - Gone
- 411 - Length Required
- 412 - Precondition Failed
- 413 - Request Entity Too Large
- 414 - Request-URI Too Long
- 415 - Unsupported Media Type
- 416 - Requested Range Not Satisfiable
- 417 - Expectation Failed
- 500 - Internal Server Error
- 501 - Not Implemented
- 502 - Bad Gateway
- 503 - Service Unavailable
- 504 - Gateway Timeout
- 505 - HTTP Version Not Supported
If you are interested, I have also put together a more thorough [url=http://www.addedbytes.com/apache/http-status-codes-explained/]explanation of the meaning of the HTTP Status Response Codes[/url].

25 Comments
Curious as to if it is possible to control the message that is becoming more and more common now - that is the one that says something like "the user xxxxx has exceeded there bandwidth............"
Is this a one that can be altered, or is it in the hands of the host?
#1, kensplace, United Kingdom, 25 June 2004. Reply to this.
A few people have found this page by searching for "comments in htaccess". So, in the interests of being helpful ...
To add comments to an htaccess file, you begin the line with a hash (#).
And to Ken:
Sorry, but that is a message generated by the host themselves, rather than your site, and cannot (usually) be customised.
#2, Dave Child, United Kingdom, 6 July 2004. Reply to this.
Hi,
Can you please tell me how you setup the error page to email you when there is an error?
Thanks,
Derek
#3, Derek, United States, 15 September 2004. Reply to this.
A 404 page can be a normal PHP page. You just need to add a line to that to email you the referer and requested URL, and voila, all done.
#4, Dave Child, United Kingdom, 16 September 2004. Reply to this.
Your link to the status code explanation (end of page 2) is dead, I believe it should point to "apache/http-status-codes-explained/" rather than "Development/HTTP_Status_Codes_Explained"
Just letting you know,
Cheers.
#5, Jim, Canada, 12 June 2006. Reply to this.
Hi Jim. Thanks for letting me know about this. I changed the site URL structure in 2005, and missed a couple of changes. All fixed now though.
#6, Dave Child, United Kingdom, 12 June 2006. Reply to this.
works very vell.
#7, iddl, Jordan, 17 July 2006. Reply to this.
Hi, nice site, very beautifull!
#8, Jonn34, Spain, 16 August 2006. Reply to this.
I got the error page to work just fine, but it only displays correctly (with CSS and images) if the error is caused in the same directory as error.php because of the relative links I used. So if I look for the page http://example.com/dir1/not_here that doesn't exist and error.php is in the root directory, all of the links say something like images/picture.png instead of ../images/picture.png and I just get the plain HTML. Is there a good way to fix this? Is it even an OK idea to have pages in different directories like that? Thank you very much!
#9, Ryan, Unknown, 19 August 2006. Reply to this.
thanks for the "comments in .htaccess" tip, that is indeed how i came here (-;
#10, dave, United States, 26 August 2006. Reply to this.
Very good site!!! I am happy on thiz place!!!
#11, Hillary, China, 28 August 2006. Reply to this.
In response to my own comment (#9), I came-up with a solution that works for me using $_SERVER['DOCUMENT_ROOT'] for include() or require() and an absolute path (like, "/project/") for paths to be written into the HTML. There are more details at thread I linked with my name.
#12, Ryan, Unknown, 30 August 2006. Reply to this.
Hello, searched in searchengine for one, and has found absolutely another, happens such!
#13, Dimas, Spain, 1 September 2006. Reply to this.
Guten Tag, biete ich die Zusammenarbeit an, wie sich mit Ihnen zu verbinden?
#14, Denissen, Spain, 2 September 2006. Reply to this.
Just what I was looking for ;)
Instead of using errordocument /404.php (or something else), I wanted to use just a message so it kept it the same URL...lile just in case they mispelled the address ;)
#15, Anders, Norway, 6 February 2007. Reply to this.
Thanks for this great tutorial!
#16, Anders Moen, Norway, 12 April 2007. Reply to this.
"For example, my 404 (page not found) error page also sends me an email whenever somebody ends up there,"
First of all, I'd like to apologize for the two emails you just got because I opened http://www.addedbytes.com/apache/htaccess-error-documents/wtf_is_this?
Second, although this is a good tutorial on how to work with error documents, your's says the search box is on the left... (left is the hand one that makes an "L" out of your thumb and index finger, not the one that makes a "_|", whatever that may be.)
Anyway, great site. I love the cheat sheets btw.
#17, Amazing Ant, Unknown, 2 May 2007. Reply to this.
Haha, nicely spotted Ant. This is, of course, over three years old now and the design has changed a lot since back then. Also, emails are no longer sent - there were just too many to be useful after a while.
Glad you like the site though.
#18, Dave Child, Unknown, 3 May 2007. Reply to this.
Hehe, so do you blog on the site anymore or? :)
#19, Anders Moen, Norway, 13 May 2007. Reply to this.
I tried putting "ErrorDocument 404 /path/to/your/error/file/" in my .htaccess file, and it worked just fine except it returns the status code of 200, which Google doesn't like. In fact, it made my site become unverified with Google. I've seen at least one other webmaster with the same problem. Is there some way to force the status 404 to remain and still offer up a custom error page?
#20, Susan Lucas, Unknown, 30 August 2007. Reply to this.
Okay first thing:
One of you error code definitions is WRONG 302 is temporary redirect NOT 307
Also @ Susan Lucas
With PHP you can set the header using header(status 404); or something like this but you cannot (as far as i know) change the "state" of the server using php or any file you can pretend it is and try to tell the broswer it is but if you want the custom error it kind of overrides it and becuase it still serving a page its technically not not found if that makes sense.
Dan
#21, Dansgalaxy, United Kingdom, 14 December 2007. Reply to this.
Technically 302 means "Found" - as in, the document isn't here but for now it's there. Same thing really. 307 means "temporary redirect" (though is very rarely used).
#22, Dave Child, United Kingdom, 15 December 2007. Reply to this.
cool
that is useful
#23, khaled, uk, 14 February 2009. Reply to this.
I'd love to see a .htaccess Cheatsheet on this page, its really missing in my collection ;-)
#24, MightyUhu, Germany, 4 March 2009. Reply to this.
I have 2 different sites right now, my public one and my testing one. I redirect all my 404 errors from my testing site to a page on my real site. This works OK, but when I want to get the URL of the page that the person tried to go to, I can't because it redirected. (Cross-domain results in a full redirect, so the URL of my 404 page shows in the address bar). Is it possible to add (in .htaccess):
ErrorDocument 404 http://mp4downloader.host22.com/404?url=%theURL%
(where %theURL% could be something like example.com/nonexistant.html)
Email me at jake33 at rocketmail dot com
#25, Jake, United States, 20 June 2009. Reply to this.