Handle 404, 500, and 301 Errors as Well as Many Others With an .htaccess file

Lets suppose you wanted to redirect users to your home page if they attempted to access a page that was NOT on your server. There is a very quick and simple way to do this. Its called a 404 redirect using an .htaccess file. You simply create a new file in Microsoft Notepad and type this: “ErrorDocument 404 /index.php” (Without the quotes). Then you simply save the file as .htaccess and upload it to your root directory. From now on when someone visits www.yoursite.com/chickentastes/good.html they will automatically be redirected to your homepage.

Interestingly enough you can also redirect to a specific page such as a custom 400 error page that you code up in php or html or whatever. See examples below. Just replace the “/errors/badrequest.html” part with “/myCustom400ErrorPage.php” and you’ll be fine.

ErrorDocument 400 /errors/badrequest.html ErrorDocument 401 /errors/authreqd.html ErrorDocument 403 /errors/forbid.html ErrorDocument 404 /errors/notfound.html ErrorDocument 500 /errors/serverr.html

Please find below a pretty copmplete list of server returned errors.

Successful Client Requests
  • 200 OK
  • 201 Created
  • 202 Accepted
  • 203 Non-Authorative Information
  • 204 No Content
  • 205 Reset Content
  • 206 Partial Content
Client Request Redirected
  • 300 Multiple Choices
  • 301 Moved Permanently
  • 302 Moved Temporarily
  • 303 See Other
  • 304 Not Modified
  • 305 Use Proxy
Client Request Errors
  • 400 Bad Request
  • 401 Authorization Required
  • 402 Payment Required (not used yet)
  • 403 Forbidden
  • 404 Not Found
  • 405 Method Not Allowed
  • 406 Not Acceptable (encoding)
  • 407 Proxy Authentication Required
  • 408 Request Timed Out
  • 409 Conflicting Request
  • 410 Gone
  • 411 Content Length Required
  • 412 Precondition Failed
  • 413 Request Entity Too Long
  • 414 Request URI Too Long
  • 415 Unsupported Media Type
Server Errors
  • 500 Internal Server Error
  • 501 Not Implemented
  • 502 Bad Gateway
  • 503 Service Unavailable
  • 504 Gateway Timeout
  • 505 HTTP Version Not Supported

Print This Post
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)

If you enjoyed this article please consider leaving a comment or subscribing to the feed and get future articles delivered to your feed reader.

You May Also Enjoy One Of These Posts:

Be Sociable, Share!

One Response to “Handle 404, 500, and 301 Errors as Well as Many Others With an .htaccess file”

  1. Gravatar IT consulting in Los Angeles

    Most of the people i know prefers to get a specialist’s help when it comes to this kind of errors. I think this is the best thing to do when you really don’t know how these things work, or at least you should get some assistance for all this kind of changes you want to have on your website.

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>