Introduction: Custom 404 Error Page in PHP
Every website gets the occasional, frustrating Error 404: Not Found. And if you have your own website, you may wish to customize these error pages. Thankfully, it's not that hard to do. Your error pages may be of any extension you want. Usually, they are written in SHTML. But SHTML isn't very dynamic in terms of what can be done with it. So I went over to PHP for my error pages. The coding wasn't hard either. So let's begin.
Step 1: Requirements
If you have a website, your hosing server should have PHP installed. If not, ask your server's admin if they would be kind enough to install it. If you are just screwing around wasting time, you need some type of emulator. If you're on Windows, use easyPHP for this. If you're on Linux and can spare the resources, get: apache2 and php (for Ubuntu, sudo apt-get install apache2 php). If you're on mac, I have no idea what you can use.
Once you meet these requirements, go to the next step.
Step 2: .htaccess
ErrorDocument 404 /404.phpAnd while you're at it, if you feel like it:
ErrorDocument 400 /400.php
ErrorDocument 401 /401.php
ErrorDocument 403 /403.php
ErrorDocument 404 /404.php #Don't re-add this line
ErrorDocument 500 /500.php
Step 3: 404.php
<?php echo $_SERVER['REQUEST_URI']; ?> does not exist, sorry.
The line above will tell the visitor that the page they want, along with the page's path (preceded with a slash), does not exist. It's helpful to tell the specific page because the hyperlink they followed, if they followed, may not reflect the page's path. The next code will grab if there was a referrer and who it was.
<?php
if(isset($_SERVER['HTTP_REFERER']) && !empty($_SERVER['HTTP_REFERER'])){
$refuri = parse_url($_SERVER['HTTP_REFERER']); // use the parse_url() function to create an array containing information about the domain if($refuri['host'] == "your-domain.com"){
//the link was on your site
}
else{ //the link was on another site. $refuri['host'] will return what that site is
} } else{
//the visitor typed gibberish into the address bar
}
?>
On my site, I told the user one of three things to do as per the code. If the referrer was my site, email me and let me know. If the referrer was on a different site, email them and let them know. If they types randomly in the address bar, stop doing that.
<?php
if(isset($_SERVER['HTTP_REFERER']) && !empty($_SERVER['HTTP_REFERER'])){
$refuri = parse_url($_SERVER['HTTP_REFERER']); // use the parse_url() function to create an array containing information about the domain
if($refuri['host'] == "cutlery-in-the-toaster.com"){
echo "You should email fork@cutlery-in-the-toaster.com and tell me I have a dead link on this site.";
}
else{
echo "You should email someone over at " . $refuri['host'] . " and let them know they have a dead link to this site.";
}
}
else{
echo "If you got here from Angola, you took a wrong turn at Catumbela. And if you got here by typing randomly in the address bar, stop doing that. You're filling my error logs with unnecessary junk.";
}
?>
Step 4: Testing
First, go to your site like normal. It should show up normally. To test if your .htaccess is being read, insert random junk anywhere inside it and save it. Reloading the page should give a 500 error. If not, make sure your site is set up to use .htaccess files (I just had to edit my server's config files to get it to work). If it still don't work, try deleting all the blank spaces and reinserting them.
Else, undo the junk and re-save it. Now try to visit a non-existent page. You should see your 404 page. Add a few dead links on your site and try to follow them. You should end up with the same 404 page but with different content. Add a dead link to another website and the 404 page will have different content.
Step 5: Files
Here are the files used in this ible. Edit as desired/required.
Attachments
1 Person Made This Project!
- davi koth made it!
Comments
5 months ago on Step 5
shtml files can be informative, provide useful links, and can comply with the look-and-feel of the site. What you have here looks like CLI operator interactions and really isn't what the web is about, it's 1970s-1980s internet. This may be accurate, but it is incomplete.