ASP.NET, HTTP 404 and SEO

Posted by paxer on ASP.net Weblogs See other posts from ASP.net Weblogs or by paxer
Published on Mon, 31 May 2010 02:04:00 GMT Indexed on 2010/05/31 3:23 UTC
Read the original article Hit count: 682

Filed under:
|
|
|

The other day our SEO Manager told me that he is not happy about the way ASP.NET application return HTTP response codes for Page Not Found (404) situation. I've started research and found interesting things, which could probably help others in similar situation.

 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!

 

© ASP.net Weblogs or respective owner

Related posts about seo

Related posts about 404