404 redirect using htaccess

Wednesday, February 17, 2010 2:05
Posted in category PHP

How do I make it so instead of a 404, the home page or any specified page comes up?

Here is simple trick for that ..

You will probably want to create an error document for codes 404 and 500, at the least 404 since this would give you a chance to handle requests for pages not found. 500 would help you out with internal server errors in any scripts you have running. You may also want to consider ErrorDocuments for 401 – Authorization Required (as in when somebody tries to enter a protected area of your site without the proper credentials), 403 – Forbidden (as in when a file with permissions not allowing it to be accessed by the user is requested) and 400 – Bad Request, which is one of those generic kind of errors that people get to by doing some weird stuff with your URL or scripts.

In order to specify your own customized error documents, you simply need to add the following command, on one line, within your htaccess file:

ErrorDocument code /directory/filename.ext
or
ErrorDocument 404 /errors/notfound.html
This would cause any error codeĀ  resulting in 404 to be forward to yoursite.com/errors/notfound.html

Likewise with:
ErrorDocument 500 /errors/internalerror.html

You can leave a response, or trackback from your own site.

3 Responses to “404 redirect using htaccess”

  1. scratchth says:

    April 1st, 2010 at 11:04 pm

    It is useful to try everything in practice anyway and I like that here it’s always possible to find something new. :)

  2. Exodus says:

    April 17th, 2010 at 3:42 am

    Your blog keeps getting better and better! Your older articles are not as good as newer ones you have a lot more creativity and originality now. Keep it up!
    And according to this article, I totally agree with your opinion, but only this time! :)

  3. hospedagem de site says:

    April 19th, 2010 at 9:54 pm

    this post will help me a lot for my work in college.

Leave a Reply