php[architect] logo

Want to check out an issue? Sign up to receive a special offer.

Bing Powered 404 for non-WordPress websites

Posted by on June 17, 2010

Ok, after my last post on using the Bing Search Wrapper for handing 404 errors in WordPress I had several people tell me that while they liked the idea, they didn’t want to install WordPress just so they can have intelligent 404 errors. Several other people pinged me and suggested a way that you could use the Bing Search API to handle 404’s without WordPress. As a public service, I am going to try and bridge the gap between these two groups. Going on leads provided by friends, I have hammered out a solution that works with Apache and PHP.

Step 1: Write a 404 Handler page.

This technique is predicated on the fact that Apache lets you define your own pages for handling every error. In this example We will define a script, name it 404.php and place it in the web root.

You will notice that this is a very plain, black and white page. Feel free to decorate it to taste. The one on my personal site actually designed using my WordPress template so it matches my blog. (you can see it by clicking here,

If you want to skip this step, here is a simple one, just copy and paste it. Make sure you put in your domain name and your Bing App Id

function __autoload($className)
   $fileName = strtr($className,'_',DIRECTORY_SEPARATOR).".php";
   include $fileName;

   $o = new Msft_Bing_Search('BING APP ID GOES HERE');
     ->setSite('YOUR DOMAIN GOES HERE')

   $raw    = $o->search();
   $result = json_decode($raw);

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="" dir="ltr" lang="en-US">

<head profile="">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

<title>calevans dot com 404 page</title>

<h3> I'm sorry, we couldn't find what you are looking for.</h3>
<p>However, here are the top 10 links from <a href="">Bing</a> for this site. I hope one of them is what you are looking for.</p>
       foreach($result->SearchResponse->Web->Results as $value) {
           printf('<li><a href="%s">%s</a></li>',$value->Url,$value->Title);

Step 2: Configure Apache

If you are not familiar with Apache config files, this one is going to be a bit tricky. The only generic advice I can give you is, back everything up, they are only text files. Unless you delete things without backups, it’s really hard to do permanent damage. Oh and if you do manage to do permanent damage, I am not responsible.

My Apache is configured with name based virtual hosts. As such I have a separate Apache conf file for each domain and all the settings go in those files. As such, inside my <VirtualHost tag I place the line:

ErrorDocument 404 /404.php

Now restart Apache and you too can type in:

and get something that looks like this:

Or, if you are lucky enough to have a designer like the lovely and talented Kathy Then you may have something that looks like this:

Cal Evans is a veteran of the browser wars. (BW-I, the big one) He has been programming for more years than he likes to remember but for the past [redacted] years he's been working strictly with PHP, MySQL and their friends. Cal regularly speaks at PHP users groups and conferences, writes articles and wanders the net looking for trouble to cause. He blogs on an "as he feels like it" basis at Postcards from my life.
Tags: , , ,

Responses and Pingbacks

[…] See the article here: Bing Powered 404 for non-WordPress websites | php|architect […]

Very helpful information. Thanks for the great share!

Hi, can these codes work on magento?


Yes but since I am not familiar with the internals of Magento, I can’t show you how.


Leave a comment

Use the form below to leave a comment: