Introduction
As a website owner, you should regularly check your error logs. Doing so helps you notice if one of your page isn't working properly or if you've misspelled a link.However, it also happens that other people link to your website with a wrong link. The first impression is always important - and starting off with an error message is definitely bad. Think about the average surfer: he follows a link which leads to an error page on your website. It's more likely for him to think that you've removed the page rather than thinking that the link was bad, so he gives up and doesn't look for the resource through your menu. Fortunately, some bad links can be corrected automatically without much trouble.
The Problem
Checking my error logs, I've noticed there are a few common forms of malformed links. My custom error page now recognizes common forms of link malformation and automatically redirects the user to the correct URL, without any error messages and without the user noticing.First, let's look at the most frequent link errors, illustrated with the link http://korn19.ch/coding/days.php.
1) Superfluous Characters
The requested link is something like http://korn19.ch/coding/days.php> or even http://korn19.ch/coding/days.php>Click. This error is usually caused by bad HTML or by a software bug.
2) Multiple Dots
The requested link contains two consecutive dots: http://korn19.ch/coding/days..php.
3) Comma or Slash Instead of Dot
The link has commas instead of dots: http://korn19.ch/coding/days,php. Or the file extensions is preceded by a slash instead of a dot: http://korn19.ch/coding/days/php.
The Solution
Create a custom error page, say, 404.php. (Tutorials about "how to create a custom 404 error page" can be found via Google.) At the beginning of your new custom error page (e.g. 404.php), add this:
<?php
$file = $_SERVER['REQUEST_URI'];
# Error 1
if(preg_match('/.*\.php(.+)$/', $file)){
header('Location: '.preg_replace('/^((.*)\.php)(.*?)$/', '\\1', $file));
exit;
}
# Error 2
if(strpos($file, ',') !== false){ #comma ,
$file = str_replace(',', '.', $file);
header('Location: '.$file);
exit;
}
if(preg_match('/.*\/php$/i', $file)){ #slash /
$file = preg_replace('/(.*)(\/php)$/i', '\\1.php', $file);
header('Location: '.$file);
exit;
}
# Error 3
if(strpos($file, '..') !== false){
$file = preg_replace('/(\.){2,}/', '.', $file);
header('Location: '.$file);
exit;
}
?>
After this code, add all of the content and code you want for your custom error message.
The above code might cause undesired behavior if you use mod_rewrite or other types of URL forms which the average website
doesn't have. Please read below.
Regardless of your file names, the code above will NOT redirect users to another page if they weren't going to see an error in the first place. However,
this code isn't ideal if you have file names with commas or multiple dots on your website, or if you have a folder called /php since it might redirect the
user to [link].php instead, which you wouldn't want to do. If there is one error described above you wouldn't want to "fix", you can remove the code block
which fixes it - the comment (the orange text that says "Error 1-3") tells you which block to remove.
The above code is for websites which use .php as default extension. The links under the code box allows you to change the code for default .html extensions or for both .php and .html. If you use different extensions and can't change the code accordingly, you can contact me.
Additional Considerations
If you are very consequent with your file names, you may want to keep an eye on your error logs and fix additional error patterns. If you always use lowercase file names, you may want to redirect a user to the lowercase variant of the URL if it isn't found:
<?php
if($file != strtolower($file)){
header('Location: '.strtolower($file));
exit;
}
?>
Even replacing all characters outside of A-Z, 0-9, hyphens (-), underscores (_) and dots (.), perhaps also parentheses, might be an idea. I don't think such a general replacement would help a lot of users who were led to a wrong URL, but if you want to get that one exception, you should consider this.
I've noticed in my logs that there is a bug in the forum software Invision Power Board (IPB). When someone searches for something on an IPB board and clicks on a topic that matched the query, the forum software highlights the keywords. This is also the case for links: http://korn19.ch/coding/days.php suddenly becomes http://korn19.ch/coding/<span class='searchlite'>days</span>.php if someone was searching the word "days". I'm not sure if this error has been fixed now - I wasn't able to find the IPB version of the referrer.
I didn't spend the time to fix this because I've only seen one error based on this; it could be easily fixed with regular expressions if this were more common, however.
Example of the Code
I am using the code in this article for my error handling. Here are some examples, based on http://korn19.ch/coding/days.php:- http://korn19.ch/coding/days.php>
- http://korn19.ch/coding/days,php
- http://korn19.ch/coding/days/php
- http://korn19.ch/coding/Days.PHP
- http://korn19.ch/coding/Days,,php">
If you have questions or know of more common errors that could be fixed, please let me know!

