ASP.NET, HTTP 404 and SEO

1) By default ASP.NET application handle 404 error by using next web.config settings

 <customErrors defaultRedirect="GenericError.htm" mode="On">

            <error statusCode="404" redirect="404.html"/>

  </customErrors>

However this approach has a problem, and this is actually what our SEO manager was talking about. This is what HTTP return to request in case of Page not Found situation.

So first of all it return HTTP 302 Redirect code and then HTTP 200 - ok code.

The problem : We need to have HTTP 404 response code at the end of response for SEO purposes.

Solution 1

Let's change a bit our web.config settings to handle 404 error not on static html page but on .aspx page

<customErrors defaultRedirect="GenericError.htm" mode="On">

            <error statusCode="404" redirect="404.aspx"/>

 </customErrors>

And now let's add in Page_Load event on 404.aspx page next lines

protected void Page_Load(object sender, EventArgs e)

            {

                Response.StatusCode = 404;

            }

Now let's run our test again

Now it has got better, last HTTP response code is 404, but my SEO manager still was not happy, becouse we still have 302 code before it, and as he said this is bad for Google search optimization. So we need to have only 404 HTTP code alone.

Solution 2

Let's comment our web.config settings

<!--<customErrors defaultRedirect="GenericError.htm" mode="On">

            <error statusCode="404" redirect="404.html"/>

          </customErrors>-->

Now, let's open our Global.asax file, or if it does not exist in your project - add it. Then we need to add next logic which will detect if server error code is 404 (Page not found) then handle it.

protected void Application_Error(object sender, EventArgs e)

            {           

                Exception ex = Server.GetLastError();

                if (ex is HttpException)

                {

                    if (((HttpException)(ex)).GetHttpCode() == 404)

                        Server.Transfer("~/404.html");

                }

                // Code that runs when an unhandled error occurs

                Server.Transfer("~/GenericError.htm");

    

            }

Cool, now let's start our test again...

Yehaa, looks like now we have only 404 HTTP response code, SEO manager and Google are happy and so do i:)

Hope this helps!

Posted by paxer Mon, 31 May 2010 13:07:00 GMT


Comments

Leave a response

  1. Avatar
    SEO almost 4 years later:

    From the SEO perspective, these pages are considered fatal errors as they affect the SEO positioning and offer a bad user experience. The 404 page customization is vital for positioning the website as the fact that a response of this type of server (self-page) account as an error when performing search engine crawling. In addition, by offering the user-oriented find what you were looking page, your experience is enhanced during navigation and prevents exit the website.

Leave a comment