ASP.NET, HTTP 404 and SEO

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!

 

27 Comments

  • Solution 3: (.NET 3.5 SP1 or higher)





    http://stackoverflow.com/questions/152307/google-404-and-net-custom-error-pages
    http://msdn.microsoft.com/en-us/library/system.web.configuration.customerrorssection.redirectmode.aspx







  • This will fix the problem only if you redirect to .aspx page and on this page you have Response.StatusCode = 404; in Page_Load In case if you need to redirect to static html file this method does not work for SEO because it return HTTP 200.

  • Just curious. Why would your SEO Manager want the 404? I would think that you would want a 302 to a new page. Obviously I am missing the SEO perspective in this discussion. I would be very curious to learn...thanks in advance.

  • Beau, the SEO manager would want Google to remove non-existent URLs from its index.

  • What program / plugin are you using to see the response codes?

  • All good, but if you are using rewriting then you can simply leave all error handling as is and then detect page in url. This then sends a 404 whenever the page hits the /oops page. Then just redirect this page from within your handler when page not found. Not as clean in so far that it will return a 302 first, but not that big a deal as more important thing is to have the 404 at the end.



    eg. in page load have

    if (!Page.IsPostback) {
    if (Request.RawUrl.ToString().ToLower().IndexOf("/oops") >= 0)
    {
    Response.StatusCode = 404;
    }
    }

    works a treat!

  • Just wish to say your article is as astonishing. The clearness in your post is simply spectacular and i could assume you are an expert
    on this subject. Well with your permission let me to grab your RSS feed to
    keep updated with forthcoming post. Thanks a million and
    please continue the gratifying work.

  • Informative article, totally what I wanted to find.

  • Great article.
    Is your theme custom built or did you download it from somewhere?

    A theme like yours with a few simple tweaks would really make my blog look more pro.
    Please let me know!
    Thank you

  • Easy to follow - quick & effective solution.

  • Nice article.Its a very useful information to sharing this article.Thank you !

  • We stumbled over here coming from a different website
    and thought I might check things out. I like what I see so now i am following you.
    Look forward to checking out your web page yet again.

  • I go to see day-to-day a few blogs and blogs to
    read content, but this webpage provides feature based articles.

  • What's up it's me, I am also visiting this web site
    regularly, this web page is truly nice and the viewers are actually sharing good
    thoughts.

  • I enjoy what you guys are usually up too. This type
    of clever work and coverage! Keep up the wonderful works guys I've included you guys to blogroll.

  • Wow, wonderful blog layout! How long have you been blogging for?
    you make blogging look easy. The overall look of your web
    site is fantastic, let alone the content!

  • Hola! I've been following your web site for some time now and finally got the bravery to go ahead and give you a shout out from Porter Tx! Just wanted to mention keep up the excellent work!

  • Hello to all, how is the whole thing, I think every one
    is getting more from this site, and your views are nice in favor of new visitors.

  • Right away I am going away to do my breakfast, when having my breakfast coming again
    to read other news.

  • You made some decent points there. I checked on the internet
    for more info about the issue and found most people will go along
    with your views on this site.

  • Amazing! This blog looks exactly like my old one!
    It's on a totally different subject but it has pretty much the same page layout and design. Excellent choice of colors!

  • Now I am going away to do my breakfast, after
    having my breakfast coming yet again to read additional
    news.

  • Howdy! I simply wish to offer you a huge thumbs up for the great info you have right here on this post.
    I am coming back to your website for more soon.

  • Ganz neuer SEM + SEO Business Marktplatz.

  • Hi there Dear, are you actually visiting this website on
    a regular basis, if so then you will absolutely take fastidious knowledge.

  • I am now not positive the place you are getting your info, but
    good topic. I must spend some time learning much more or figuring out more.
    Thanks for wonderful info I used to be on the lookout for this information
    for my mission.

  • I am sure this article has touched all the internet viewers, its really really fastidious post on building up new web site.

Comments have been disabled for this content.