Welcome, Guest. [ Log In ]
Question   Custom 404 (File Not Found) error pages.
Search KBase


Related Links:
· Custom 403 (Forbidden) error pages.
· Custom 401 (Failed Authorization) error pages.
· Custom 500 (Internal Server Error) error pages.

Top 4 in this Area:
1. Custom 404 (File Not Found) error pages.
2. Custom 403 (Forbidden) error pages.
3. Custom 401 (Failed Authorization) error pages.
4. Custom 500 (Internal Server Error) error pages.

 
Custom 404 (File Not Found) error pages.
I've noticed that some people have created a customized web page that appears when you try to access a page on their site that does not exist. How can I do that?

404: File Not Found

When a visitor to your site attempts to load a page that does not exist, they receive a generic error message stating that the requested page cannot be found. This is what is known as a '404 error', and is often discovered through broken links to to pages on your site.

The Solution

Of course, the first thing you want to do is make sure that all links within your site are accurate. Your visitors will thank you for it.

However, that won't catch incorrect links from other sites or the occasional mistyping of a URL. To catch these kinds of errors, you need to create a custom 404 error page, using the same methods you would to create any other page. Then, name it 'missing.html' and upload it to the root level of your domain. From that point on, it will replace the generic '404: File Not found' message that your visitors will encounter.

Tips

First off, your custom 404 error page should state something to the effect of why the expected page did not appear. This will clue your visitors in to the fact that they should probably back up and try again, or report a broken link if they got to the error from a link on your site.

Internet Explorer doesn't (by default) display custom error pages, preferring to display its own error message. If you would like to override this, you can put hidden text in the page to increase the filesize to just over 512 bytes; IE will then display your custom error page.

You may also want to provide some links to the main sections of your web site, to help them move on to the area they want to view. You can consider this to be a helpful guide that appears when you look lost - a site map or directory would probably be quite welcome by that point. If you are feeling especially helpful or ambitious, you might want to even consider adding a search engine to this page to help your audience find its way.

Last but not least, you should provide absolute links, such as:

A HREF="http://mydomain.com/info/"

...instead of relative links, such as:

A HREF="../info/"

...so that these links don't become broken at any location of your site. Remember, since 404 errors can occur anywhere on your site, you can't count on them only happening within a given directory structure.

Last updated: Oct 04, 2001.

User Post (2005-11-26 15:38:11 by wrongname)
See the following article in the Dreamhost Wiki for a solution that takes into account most of the user suggestions on this page.

http://wiki.dreamhost.com/index.php/Custom_error_pages
User Post (2005-10-30 16:41:29 by carnaticwm)
404 research lab contains useful info - http://www.plinko.net/404/

I use Shaun's 404Handler - http://shat.net/php/404/ - with minor changes
User Post (2005-03-18 20:49:39 by gailgrant)
Gail's Tutorial on Customized Error Messages

While the other things listed here work, I like all my custom error messages in a single directory. If that is what you want, read on. All stuff in parens are the shell commands to do what I've told you to do.

1. Create a file in the root directory of your web server named .htaccess that contains:

ErrorDocument 400 /errors/400.html
ErrorDocument 401 /errors/401.html
ErrorDocument 403 /errors/403.html
ErrorDocument 404 /errors/404.html

and make sure that the protection on the file allows world read access (chmod 644 .htaccess). NOTE: Most web browsers won't let you look at .htaccess, even though it has world read permission.

2. Create a directory named errors (mkdir errors) and set the protections to world read/execute (chmod 755 errors)

3. Inside that directory, create
400.html
401.html
403.html
404.html
and make sure they are world readable (chmod 644 *.html, if you are in that directory)

These files should explain to the user what the error message means in plain English. It is also a good idea to give them pointers to at least your home page and possibly to the most popular pages on your site. Go to: http://www.yoga4good.com/errors/ to see the ones on one of my sites.

4. Test them out by triggering the error codes.

If you have problems, you can always check your error.log to see what errors are being generated.

Hope this helps!

Gail
User Post (2004-10-28 16:16:31 by dage)
If you're having trouble getting your error pages to work in Internet Explorer, you may be triggering the "Friendly HTTP Error Messages" handling.

To avoid this, the content length of your "errorpage".html must be greater than or equal to 512 bytes. You can also disable this via Internet Options -> Advanced -> [x] Show friendly HTTP error messages.
User Post (2004-07-04 06:32:03 by mfr)
You can also specify a PHP file and have it handle the error. But you have to do this for each kind of error... Put this in error.php:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">

<HTML>
<HEAD>
<TITLE>[Error] Oops! An Error...</TITLE>
<meta http-equiv="REFRESH" content="40; url=http://www.yoursite.com">
<META NAME="description" CONTENT="description of your site">
<META NAME="keywords" CONTENT="many keywords">

</HEAD>

<P />
<?php
switch(getenv("REDIRECT_STATUS"))
{
# "400 Bad Request"
case 400:
$error = "400 - Bad server request";
break;

# "401 Authorization Required"
case 401:
$error = "401 - Authorization required";
$explain = "This section requires a password or is otherwise protected. If you feel you have a legitimate need to access it, please <a href=\"http://www.contactmeurl.com\">contact me</a>.";
break;

# "403 Forbidden"
case 403:
$error = "403 - Forbidden access" ;
$explain = "This section requires a password or is otherwise protected. If you feel you have a legitimate need to access it, please <a href=\"http://www.contactmeurl.com\">contact me</a>.";
break;

# "404 Not Found"
case 404:
$error = "404 - Not Found";
$explain = "This site structure has changed recently. You will be redirected to its main page in a few seconds. If you are looking for a past blog comment or post, please go to <a href=\"http://www.newaddress.com\">the new address</a>.";
break;

# "500 Internal Server Error"
case 500:
$error = "500 - Internal server error";
$explain = "Please verify the address and try again.";
break;

}

$other = " Other information you may be looking for:<P />";
$other .= "<UL><LI><a href=\"http://www.onelink.com\">Thissite.com</a> - Description</LI>";
$other .= "<LI><a href=\"http://www.anothersite.com\">someothersite.com</a> - Another descr.</LI>";
$other .= "</UL>";

echo "<H1 align=\"center\">Wow! You managed to find a flaw !</H1><H2 align=\"center\">Server says: $error</H2><div align=\"center\"><table border=\"0\" width=\"600\"><TR><TD valign=\"top\" align=\"left\">$explain<P />$other</td></tr></table></div>";
?>


<?php
// optionally...
// mail("myemail@mydomain.com", "Error ". getenv("REDIRECT_STATUS"), "Check this page: ". getenv("REQUEST_URI"));

?>
User Post (2004-01-06 02:52:27 by hunsvotti)
To define your own custom 404 handler, put this into your .htaccess:

ErrorDocument 404 /missing.shtml (or whatever... the leading / is important)
User Post (2003-12-29 20:04:52 by kmaxwell)
cyan: you might try a rewrite (I assume those get parsed first), with something like

RewriteEngine on
RewriteRule missing\.html /missing\.shtml

in your .htaccess file.
User Post (2003-06-24 14:29:53 by gpb666)
Great tip on that missing.html , however , to avoid having the directory displayed , better also create the same missing.html under the name index.html , if you intend not to have a regular index file of course
User Post (2003-05-04 10:54:35 by ratballs)
cyan:
This doesn't really answer your question but is an effective work-around.
What I did was, in .htaccess, added the following:

AddType text/x-server-parsed-html .html

This will basically parse files with .html extensions in the same way that, by default, it parses files with .shtml extensions.

Also in addition (or in lieu of) what ardco posted, if the .htaccess file is in your whatever.com directory, you don't need to specify /underMainHome/myforbid.html but rather can get away with:

ErrorDocument 403 /myforbid.html
User Post (2003-04-26 06:20:27 by cyan)
So how do we use .htaccess to set our own custom error page names instead of 'missing.html' I want to use 'missing.shtml'
User Post (2003-04-16 08:50:07 by lmdr_web)
I had a php 404 page that worked (even with the missing.html name) when I had the permissions set to 755 - but now it won't work! I must have done something when I uploaded a new version, but I have checked the permissions and they are fine.

What could be the problem?
User Post (2003-03-14 11:42:21 by jesseruderman)
If I don't want a custom 404 page, how do I get rid of "Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request." and the entries for missing.html in my error logs?
User Post (2003-02-19 07:48:32 by macat)
I tried making a custom 404 error message and if I type in the actual address it is there, but if I make an error I get the generic page. What have I done wrong?

I have placed the http://www.bemikitties.com/failed_auth.html http://www.bemikitties.com/dom/failed_auth.html in both my main directory and in the protected directory but it still doesn't work. Here is the site I'm talking about address:

http://www.bemikitties.com/don ID don password medon

Thanks for the help in advance.

You can email me at macat@bemikitties.com
User Post (2002-01-20 21:53:17 by ardco)
Works for me, at least it just did for a custom 403 error page I just tested. You can't put the custom page in a forbidden directory - that won't work. All I did was add a line to the .htaccess file in the desired directory, with something like this:
ErrorDocument 403 /underMainHome/myforbid.html
User Post (2001-12-25 01:59:10 by jlvlahos)
anybody know why it seems specifying custom error pages from within .htaccess won't work?