Valid XHTML 1.0!
Valid CSS

 

 

 

Creating a Dynamic Custom 404 Error Page

or an Index File to Block Folder Listing

Just another example, of why server side scripts (such as .php) are more powerful (when compared to a plain .html page :)

This tutorial explains how to make a Custom 404 Error Page based on a html / .php script. Actually, there are two types of pages that are suited for this example.

Custom 404 (page not found) Error Page: Where it can be used to determine when someone enters the wrong URL or when they follow a 'bad / obsolete' link to the site.
Note: Internet Explorer, IE browser will not display custom error page if the page is small i.e. if IE does not show the custom error page - increase the page size.

Index.php Page to Block Listing of Folders: Actually the php / html page will block the listing of files and/or subfolders -- such as an attempt to list the files in a folder that does not have a index.??? file.

In both cases, the .php / html code shown below will send an email notice when the page loads. This is the 'cool' part! The email will contain information showing what the visitor was looking for or what URL / referral that led them to the page. The email will include:

This example uses simple HTML code for the web page display. Of course, that makes it easy to change / design any 'custom error' page.


Instructions

Step 1: Copy n Paste the 'Code' shown below into a new file.
Step 2: Be sure to replace the 'yourdomain' (3 places) with the real domain name.
Step 3: Save the file with a unique name with a .php extension. (such as 404.php)
Steps 4 ... 6 :

For a Custom 404 Error Page:

      Upload the (404.php) file to the desired folder. (such as /errors/)
      Add the following line to the .htaccess file
                  ErrorDocument 404 /errors/404.php
see notes for .htaccess files

To Block Folder Listings:

\

      Rename the file to 'index.php'.
      Upload index.php to the folder to be blocked.
Caution: Make sure the folder did not already have an index.??? file that needs to load.

To see an example output, type some fake URL such as http://www.ibdhost.com/errors/home.html or click to try to list the images folder at: http://www.ibdhost.com/images/
And YES, a copy of the visit / attempt will be mailed to us. :)



Code


<HTML>
<HEAD>
<title> 404 Error Page</title>
</HEAD>
<BODY>
<p align="center">

<h1>Error 404</h1><br>Page Not Found

<p>
<?php

$ip = getenv ("REMOTE_ADDR");

$requri = getenv ("REQUEST_URI");
$servname = getenv ("SERVER_NAME");
$combine = $ip . " tried to load " . $servname . $requri ;

$httpref = getenv ("HTTP_REFERER");
$httpagent = getenv ("HTTP_USER_AGENT");

$today = date("D M j Y g:i:s a T");

$note = "Yes you have been bagged and tagged for a making an illegal move" ;

$message = "$today \n
<br>
$combine <br> \n
User Agent = $httpagent \n
<h2> $note </h2>\n
<br> $httpref ";

$message2 = "$today \n
$combine \n
User Agent = $httpagent \n
$note \n
$httpref ";

$to = "error@yourdomain.com";
$subject = "yourdomain Error Page";
$from = "From: fake@yourdomain.com\r\n";

mail($to, $subject, $message2, $from);

echo $message;
?>

</BODY></HTML>


.htaccess notes:

A .htaccess file is a system file that allows you to override some Apache server instructions. A .htaccess file in one directory affects all the subdirectories that do not have an .htaccess file of their own. The name must be all lowercase i.e. .htaccess

To configure a custom error page, create (or modify) the .htaccess file in the top directory by adding an ErrorDocument. The format is:

ErrorDocument ERRORCODE URL
It is best to use the relative path for the URL not a full URL.
For example, if your .php file is named 404.php and it is in a directory named errors, the line to add to .htaccess file is:

ErrorDocument 404 /errors/404.php

Warning: When creating a new .htaccess file, the resulting name may be - htaccess.txt If so, be sure to remove the extension and rename the file to .htaccess

If your FTP program is not set to "show system files" you need to set 'remote file mask' to:

-al

For WS_FTP LE, open the sessions box. Then under startup be sure to enter the -al as the 'remote file mask'. For other FTP programs, check for remote mask or an option to 'show hidden system files'

Adsense Dead

Owner Has


Gone Fishing