B&T's Tips & Scripts
B&T's Tips & Scripts is a collection of Tips & Scripts to help you build a better website. The Tips & Scripts are targeted towards the native Apache PHP MySQL environment. Check back frequently as there will be additions and modifications. Please read our terms of use and our frequently asked questions. And while you are here, check out B&T's Tool Box.

404 Error Page (with logging)

Last updated: October 29, 1012

This Custom 404 PHP script will display an error page. It also logs all the relevant information when there is a 404 error. The log file feature is handy for quicky checking 404's on your site, you can check the file via FTP or in your browser. This is useful for identifying bad links to your site and it is also a great debugging tool, notifying you of your own invalid references. The log file will be named 404.txt and will reside in the document root directory.

In order to use it you must add the following line to your .htaccess file.  DO NOT use the full URL to the error page, use the relative path as done in the example below.

ErrorDocument 404 /404.php

Assuming the script below is named 404.php and placed in your document root directory.

You can download this script as a .txt file.  Remember to rename the file as 404.php.

- - Start Script Here - -
/putenv('TZ=EST5EDT'); // eastern time
// error log
$logFile $_SERVER['DOCUMENT_ROOT']."/404.txt";
$logData date('Y-m-d H:i:s')."\t".$_SERVER['REMOTE_ADDR']."\t".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']."\n";
// output page
header('HTTP/1.0 404 Not Found');
<!DOCTYPE html>
<html lang="en">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>404 - request not found</title>
<style type="text/css">
body { margin: 0px; background-color: rgb(60,60,60) }
a:link { font-weight: bold; color: rgb(60,60,180); }
a:hover {color: rgb(180,60,60); }
<div style="margin: 100px auto; padding: 30px; border: solid 20px rgb(20,20,20); font-size: 16px; color: rgb(60,60,60); width: 550px; background-color: white;">
<span style="font-weight: bold; font-size: 28px;">request not found</span>
<br /><br /><br />(oops)
<br /><br />it appears you were looking for
<br /><span style="font-weight: bold;"><?php echo $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; ?></span>   
<br /><br />however, since you are here, it is clear you did not get what you wanted.
<br /><br />the problem has been reported so any broken links can be found and repaired.
<br /><br />you can click <a href="javascript:history.go(-1)">here</a> to go back to your previous page.

- - End Script Here - -
Rate This Topic
topic rating using this script.
You are visitor 1,538,285 (using this script)
Copyright © 2004-2014