404 File Not Found Email Notification Using PHP for WordPress Blogs and Other Websites

Do you have a 404 page in your website or self hosted blog? If not, wordpress.org has given instructions on how to create custom 404 for WordPress theme. For your website, you may just google it out.

If your hosting supports, define the 404 error handler page as 404.php in the .htaccess file, like this.

# For general website
ErrorDocument 404 /404.php

# For wordpress
ErrorDocument 404 /index.php?error=404

where 404.php is the error handler. The file will be located in the website root folder or in the current theme folder for WordPress. Now you can write PHP functions in this page to take care of your page not found requests.

There are various options like log file analysis to find out the 404 for the website and make changes accordingly. For smaller websites and blogs with very less number of visitors, you may feel that it is not worth log analysis.

And we still use email as the instant notification mechanism. I am also like that. So I setup up email notification for the 404 file not found error. Whenever someone access any webpage or other resource which is not found i this server, I get an email.During the initial days I had many emails, and then I fixed one by one and later I receive very less number of emails per day.

[advt]

Since I am using Google Apps for domain, which is in effect gmail, I label such emails with a 404 tag and archive them using filters. I check it a few times daily to see if there is something which needs real attention from my side.

Here is the script I put at the end of the wordpress 404.php file in my current sky blue theme. (I have defined this in custom_functions.php, which I have included in functions.php.)

// Email the admin about the 404 error.
function custom_404_email() {
    $request_uri = $_SERVER[‘REQUEST_URI’];

//The following conditions are used to avoid some meaningless 404 errors from being notified. 

//like: Google MediaPartners crawler for Adsense, http://www.msoffice-cltreq-asp.info/, etc.
if ( ($_SERVER[‘HTTP_USER_AGENT’] != ‘Mediapartners-Google’) &&
( strpos($request_uri, ‘/MSOffice/cltreq.asp’) === false )  &&
( strpos($request_uri, ‘/_vti_bin/owssvr.dll’) === false )  &&
( strpos($request_uri, ‘/_vpi.xml’) === false ) )

        $website_name = “WEBSITENAME“;
        $website_url = “http://yourdomain.tld“;
        $mail_to = “[email protected]“;
        $mail_from = “noreply@ yourdomain.tld“;
        $mail_subject = $website_name . ” 404: “.$request_uri;
        $error_message = “”;
        $error_message .= “Requested URL (404):   ” . $website_url . $request_uri . “\r\n”;
        $error_message .= “Referring URL: ” . $_SERVER[‘HTTP_REFERER’] . “\r\n”;
        $error_message .= “User Agent: ” . $_SERVER[‘HTTP_USER_AGENT’] . “\r\n”;
        $error_message .= “Remote Host: ” . $_SERVER[“REMOTE_HOST”] . ” – “. $_SERVER[“REMOTE_ADDR”] .”\r\n”;
        $error_message .= “Whois: http://ws.arin.net/cgi-bin/whois.pl?queryinput=” . $_SERVER[“REMOTE_ADDR”] . “\r\n”;
        $error_message .= “Cookie: ” . $_SERVER[“HTTP_COOKIE”] . “\r\n”;
        $head  = ”;
        $head  .= “Content-Type: text/plain \r\n”;
        $head = “From: ” . $website_name . “<” . $mail_from . “>\r\n”;
        mail($mail_to, $mail_subject, $error_message, $head);
    }
}

The function custom_404_email() is called at the end of the 404 page like this.

<?php custom_404_email(); ?>

Thats’s it, you are all set to receive all 404 notifications in your mailbox!


Note: There are a few 404 handler WordPress plugins also available. I generally shy away from using plugins wherever I can custom code the functionality for my theme.

Be the first to comment

Leave a Reply