How to Set the Default to a 404 Error Page in PHP
When a visitor attempts to load a page that does not exist on your website, the server will show a generic 404 "File Not Found" message. You can create a custom 404 page to provide both the visitor and yourself with more information about the error using a combination of PHP and an .htaccess file. PHP is a dynamic Web scripting language that you can use, in combination, to create a custom 404 error page on your website while the hypertext access file instructs the server to display your custom page instead of the generic 404 message.
Instructions
-
-
1
Open your HTML or text editor on your computer and create a new file.
-
2
Copy the following code into the file:
<HTML>
<HEAD>
<title> 404 Error Page</title>
</HEAD>
<BODY>
<p align="center">
<h1>Error 404</h1>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 = "You have entered the wrong URL or clicked a broken link" ;
$message = "$today \n
$combine \n
User Agent = $httpagent \n
<h2> $note </h2>\n
$httpref ";
$message2 = "$today \n
$combine \n
User Agent = $httpagent \n
$note \n
$httpref ";
$to = "ContactEmail";
$subject = "Error Alert";
$from = "From: ReturnEmail\r\n";
mail($to, $subject, $message2, $from);
echo $message;
?>
Visit our Home Page YourDomain
</BODY></HTML>
-
-
3
Change "You have entered the wrong URL or clicked a broken link" to the message you want your visitors to see and replace "YourDomain" with your actual domain URL or website name.
-
4
Replace "ContactEmail" with your actual email address so that you will receive an email message when someone lands on the 404 page. Additionally, replace "Error Alert" with the subject you wish to see in the alert email.
-
5
Replace "ReturnEmail" with the address that will appear in the "From" or "Recipient" field in your email program. You may use your own email address.
-
6
Choose "Save" from the File menu and type in "404.php." Ensure that you select "All File Types" when saving.
HTAccess
-
7
Create another new document by selecting "New" from the File menu.
-
8
Enter the following text into the blank document:
ErrorDocument 404 /errors/404.php
This will instruct your Web server to direct visitors to "404.php" if the page they try to access does not exist.
-
9
Save this file as ".htaccess.txt"
Uploading
-
10
Open your FTP program and enter your login credentials to establish a connection.
-
11
On the "Local" pane, navigate to the folder(s) containing the PHP and text file.
-
12
Right-click each file and select "Upload" to upload them to your Web server.
-
13
Right-click on "htaccess.txt" in the "Remote" pane and click "Rename." Change the file name to ".htaccess." The file may become hidden as soon as you finish this.
When the visitor encounters a 404 error, the server will direct her to "404.php."
-
1
Tips & Warnings
You can add additional text, links, images or other HTML elements to your 404 page. HTML and PHP work well together.
Generally, you want to upload the files to the base directory. For example "yourdomain.com/"
You can also upload the files to your Web host by using the file manager in your control panel.
Both files must be in the same directory on your server in order for this to work correctly.
The .htaccess may not work correctly if you upload in Binary mode. Adjust your FTP client settings to use this mode.
.htaccess only works on Apache servers.