Header of the 'Page not found' page at mavention.nl.
Recently I wrote an article about how you can create a proper ‘Page not found (404)’ page in SharePoint 2010. Using a custom HTTP Module and just a few lines of code we created a better experience for both your visitors and search engines. Today I will show you how you can improve it even further by adding search suggestions.

404 pages: why they suck and what can we do about it

Nobody likes landing on a 404 page. A 404 page tells us we did something wrong. Additionally in many cases 404 pages are a dead end: instead of providing constructive feedback or giving us alternatives they only say that we tried to request a page that doesn’t exist, period. After that it’s up to us to figure out a way out.

Imagine the following alternative: you’re navigating to a page you have found using search. Once on the site, you find out that the page you requested doesn’t exist. Although you’re not immediately finding what you were looking for, the page is intelligent enough to provide you with a number of alternatives based on the page that you were looking for. Sounds way better than a generic error message, doesn’t it?

The functionality I have just mentioned has been around for quite a few years now. For Internet-facing websites, not necessarily created on the SharePoint platform, you might want to use the Google’s 404 widget, which is very easy to implement on your site. While it’s a turn-key solution, the downside is that it uses Google’s search results, which is not something you could tweak, and it’s for Internet-facing websites only.

Since we’re talking about improving the experience of 404 pages on the SharePoint platform, why not create something similar but then use the SharePoint search capabilities instead? This is exactly what the Mavention Page Not Found Widget does.

Better 404 pages with Mavention Page Not Found Widget

The Mavention Page Not Found Widget is an extended version of the Search Core Results Web Part provided with SharePoint Server 2010. It uses the URL of the missing page (the page that resulted in a 404 error) to extract keywords and runs a search query for them to find the most similar pages.

Upon installation of the Solution Package a new Site Collection Feature called Mavention Page Not Found Widget is being installed.

Mavention Page not found widget Site Collection Feature.

After you activate the Mavention Page Not Found Widget Web Part will be added to the Web Parts Gallery under the Mavention Web Parts group.

Mavention Page Not Found Widget Web Part in the Web Parts Gallery.

As I mentioned before the Mavention Page Not Found Widget Web Part is an extended version of the Search Core Results Web Part. This means that you can configure almost everything just the same way as you would for a regular Search Results Web Part. The Mavention Page Not Found Widget Web Part allows you to configure one more setting which specifies noise words that should be excluded from the keyword query.

The Noise Words setting of the Mavention Page Not Found Widget Web Part.

Because keywords are being extracted from the URL it might help you to get better results by removing words such as aspx and pages from the search query.

The Mavention Page Not Found Widget Web Part assumes that the URL of the missing page is passed using the url query string parameter. If you are using a 404 page created using the approach I showed recently, you would have to change the code of the HTTP Module as follows to pass the URL of the missing page:

public class PageNotFoundHttpModule : IHttpModule
{
    private HttpApplication app;
    private string pageNotFoundUrl = "/pages/page-not-found.aspx";
 
    public void Dispose()
    {
    }
 
    public void Init(HttpApplication context)
    {
        app = context;
        app.PreSendRequestContent += new EventHandler(app_PreSendRequestContent);
    }
 
    void app_PreSendRequestContent(object sender, EventArgs e)
    {
        HttpResponse res = app.Response;
        HttpRequest req = app.Request;
 
        if (res.StatusCode == 404 &&
            !req.Url.AbsolutePath.Equals(pageNotFoundUrl, StringComparison.InvariantCultureIgnoreCase))
        {
            app.Server.TransferRequest(String.Format("{0}?url={1}", pageNotFoundUrl, req.Url.ToString()));
        }
    }
}

Now we have everything in place we can test if everything is working correctly. Assuming you have configured SharePoint Search on your site and have some content in the index, the only thing that you need to do is to misspell the URL of one of your pages. On your Page not found (404) page you should see some suggestions presented by the Mavention Page Not Found Widget Web Part:

Mavention Page Not Found Widget Web Part showing some suggestions for a missing page.

Summary

We all hate landing on ‘Page not found (404)’ pages. Although you might do your best to provide correct hyperlinks and never modify existing URLs it’s just a matter of time before someone else does it. Providing your visitors with a useful 404 page is a great way to improve the user experience and discoverability of your content. Mavention Page Not Found Widget helps you enhance the experience of your ‘Page not found’ page by automatically providing search results based on the URL of the missing page.

Download: Mavention Page Not Found Widget (6KB, WSP)