Hiya People,

Its really a pain when visitors come down to your blog and they see “Sorry, but you are looking for something that is not here.” The message can be anything, but top of everything it is nothing but a fail message.

And consequently, many of your potential visitors may bounce off your website from that very page, as its annoying for them. Internet is a big hub and they could find a better place to search the same content, so it is very important to retain your users if they drop into your site. That is what we call as Loyal or Returning users 🙂

You could have super-duper content on your website but as long as you do not serve it properly it is of no use and it will start to stale after a while then. So to log what users are actually failing at and you do not want it to repeat for other users as well, read this post carefully and I bet your site will do wonders then.

Ok lets try the famous Labnol Blog and i put a dummy URL http://www.labnol.org/Write for Digital Inspiration which is title for his latest post Write for Digital Inspiration.

But it shows a 404 error, well the content is present in his blog but shows a fail when put in the URL. Well there is no harm to a site like labnol, people are ofcourse not gonna bounce outta it but will put an effort to do a search for the content as its LABNOL 😛

But its a good exercise to automate the process instead let your user search the string manually. Your website should be intelligent enough to capture the user’s failed input and pass it on a search string to perform a successful automated search, such that 404 error shows up only if the search string is super pathetic.

Ok , So how do we do it ?

We are not only going to handle such fail requests, instead we are going to make a fail-log of the fail requests on your site, so that you can actually analyze that. On analyzing you can actually handle such failed requests and act accordingly, mostly using .htaccess

1) Handle 404 in .htaccess

We put a single line in our root’s htacess file, which will redirect all 404 errors to a particular PHP file, which is going catch all those exceptions.

ErrorDocument 404 http://www.domainname.com/my_404_handler.php

2) Put logic into PHP handle file to talkback

Ok this is the magical file which handles the bad URI and passes onto the search string further. here is the code

$stamp=date('d-m-Y H:i:s',time());
$str="Failed Query on YourSiteName ($stamp) -- $fail_query";
//will never go beyond this
function sanitize_query($fail_query)
  return str_replace($charList, " ", $fail_query);
<h1>Invalid URL / Request </h1> Please try search.

Please create a blank fail-query.log in the desired destination, as specified in above PHP lines.

If you follow the above steps, then you are perfect to go. Any problem just leave a comment here.

Hope that helps

Sachin Khosla

Share this post: