Archive for the ‘custom404’ Category

Get Notified of 404 Errors

Saturday, January 13th, 2007

One addition I have found useful is to add email notification when my custom 404 page is invoked. Since I use a PHP file for the custom 404 adding email notification is pretty easy. I renamed the original custom404.htm to custom404.php then added the email code.

The email sent will spell out the invalid page that caused the 404 error. BTW, 404 is the error generated by HTTP when an invalid or nonexistent web page is trying to be accessed. The commented out echo code was used for diagnostics.

Note that nothing is shown on the custom 404 page. The email occurs behind the scenes.

Here is the code:


<?php
// send error email
$em = "someone";
$em .= "@";
$em .= "mydomainname";
$em .= ".com";

$subject="Custom404";
$message="A custom 404 page was accessed from mysecurepc site";
$message .= " The page accessed was: " . $_SERVER['REDIRECT_SCRIPT_URI'];

if(mail("$em","$subject","$message","From: $em")){
//echo "e-mail sent !!!";
}else{
//echo "error !!!";
}
?>

A few things I have noticed since I set this up on a couple of web sites for testing.

1. For some reason, I get a custom 404 email when a bookmark like

...domain.com/folder/webpage.html#topofpage

is accessed. I have not been able figure out why, yet.

2. If you do not have a favicon.ico then the custom 404 page is invoked and you will get email. This occurs when IE accesses the home page.

3. Deleted web pages may show up in an 404 email since search engines retain these links for a while. The searchbots invoke these.

4. Deleted or name-changed web pages may show up because someone is using an old link.

Overall it has given me valuable feedback despite some unwanted emails.

Doug

Got Custom 404 Page?

Sunday, November 12th, 2006

I am surprised at how many websites do not have custom 404 pages.
Try entering a nonexistent web page for your website.
And what is a custom 404 page? A response to a web page not found. If a web page is not found on your site, the server returns a 404 error. Normally the web host has a default web page in response to a 404 error. Most web hosts allow a custom 404 page which you can put just about anything you want on it. And a link to the homepage can be put on; something a default 404 page does not have.

Check with your web host on how to redirect a 404 to your custom 404 page. Each host has its own peculiarities.

The .htaccess file can be modified to point to a custom 404 page (note .htaccess file is only for Apache servers). Add the following line:

ErrorDocument 404 http://www.mydomain.com/404-error-page.php

Replace mydomain with your domain. Note the error page can be an html file instead of a php file. This worked fine if the error page was an html page but did not work if it was a php page.

A better way is this:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) /errordocument.htm

where /errordocument.htm is the custom 404 page for your site. This can also be a php script such as custom404.php. Note this goes in the root directory. Just add directories like this: /dir1/dir2/errordocument.php

This method worked fine for nonexistent html and php pages.
Doug