SEO Friendly Error Handling.

Http Error Handling Search Engines will Love You!

One of the major problems with .Net error handling is that it returns invalid responses to search engines for common operations such as 404 missing pages and server errors. Take the following configuration, for example:

<customErrors mode="on" defaultRedirect=""~/error.htm">
  <error statusCode="404" redirect=""~/page-not-found.htm"/>
</customErrors>
		

If you request a url from a website that cannot be found, you will receive a 301 temporary redirect and then a 200 success, which is the success of the actual 404 page being returned to the browser. This is very bad from an SEO perspective as search engines will potentially index the content of the 404 page.

What we need is a 404 response immediately upon requesting a missing page.

DNAide can handle this for us!

<dnAide>
  <web>
    <httpErrorModule enabled="true">
      <rules>
        <add code="404" handlerPage="~/Templates/Errors/404.aspx" />
        <add code="500" handlerPage="~/Templates/Errors/500.aspx" />
      </rules>
    </httpErrorModule>
  </web>
</dnAide>
		

Used in conjunction with our error IHttpModule.

<httpModules>
  <add type="DNAide.Web.UrlRewriter" name="UrlRewriter" />
  <add type="DNAide.Web.HttpErrorModule" name="HttpErrorModule" />
</httpModules>
		

The error web forms exist as physical pages in the website, which we feel gives you greater control over their layout and reduces the risk of an error being thrown by an error page, as they are just static HTML!

In their code behinds, we set the Http status code to 404, which you can see in action from Fiddler.

Try it.

You'll not find me!
Throw an error!

Next Steps:

You can build on our simple error catching module by overriding the HandleError method and pointing your web.config to your own implementation.

...

protected override bool HandleError(int code, HttpException error)
{
  if (code == 500)
  {
    // Log error to an email address or central bug tracking system
  }

  return true;
}

...
		

This opens up a world of options for all your error tracking needs, whilst keeping the search engines happy!