• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Coding Still

  • Home
  • About

Handling redirections, missing pages and server errors in asp.net

May 19, 2011 By _tasos 1 Comment

In a CMS application it is really common for each authors to add, move and delete pages. In a busy and popular website this can happen really often and it could have a negative effect on how search engines crawl and index your site. It is important to help search engines understand these changes on your pages and index correctly your website.

Every http request has a status code that can give us useful information. There are 5 groups of status codes, each group has a specific context. The groups are the following. Fow more information on http status codes check the wiki article List of HTTP status codes. The most common are 200 (All is ok), 301 (moved permanently), 404 (page/file not found) and 500 (internal server error).

  • 1xx Informational
  • 2xx Success
  • 3xx Redirection
  • 4xx Client Error
  • 5xx Server Error

Using status codes we can make search engines understand what is going on with our pages. The cases we will investigate in this article  in order to make our asp.net application to be more SEO friendly regarding content management will be the following:

  • Missing pages and 404 status code.
  • Application errors and 500 status code.
  • Relocated pages and 301 status code.

By default first two cases are handled by IIS. If a request is about a page that doen’t exist, the web server gives the 404 error code. Also, if your application has an error and the Server Error in ‘/’ Application appears the status code of your page is 500. Regarding search engines all is ok and there is not much to be done here. But for your visitors it would be nice to see something more informative than the ugly error page or the IIS not found message.

There a few options in the Web.config you can use handle these issues. For example you can do the following:

<customErrors redirectMode="ResponseRewrite" defaultRedirect="error.aspx">
    <error statusCode="404" redirect="not-found.aspx"/>
</customErrors>

We set a default error.aspx page to appear when an error occurs. But for the 404 status code, we want to handle this case differently, so we set a different page to appear for missing pages. The above setup works really well (I really like the option redirectMode=”ResponseRewrite”, because the url doesn’t change) but if you check the response headers the page has is 200. This behavior will confuse search engines since they will treat theses special pages are regular pages. 500 errors are something we must eliminate so usually they are not permanent (by fixing all bugs…). But for links that are removed the 404 status code is important for search engines to stop crawl and index those pages.

Note: If you have an ASP.NET MVC project and follow this approach you might need to comment the filters.Add(new HandleErrorAttribute()); line from your ~/App_Start/FilterConfig.cs file.

It can be done very easily with ASP.NET to set the status code for the page that loads. In the Page_Load(…) event of error.aspx and not-found.aspx you can add the following line

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'The line below is required if you get IIS error code pages
        'when setting the status code programmatically
        Response.TrySkipIisCustomErrors = True
 
        Response.StatusCode = 404
        'Continue with loading the not-found page
    End Sub

This simple approach gives the desired behavior in your asp.net application. In the not-found page you could put a search form and suggest to your viewer to use it (like WordPress does!).

Update: I noticed that in some servers when you set the status code programmatically, the IIS default error pages appear. To overcome this, set the TrySkipIisCustomErrors property to True.

Finally, when you are moving your pages (e.g. changing the url that the page loads) the old url is probably out there. So, with our previous approach our application would show the not-found.aspx page. If we have a way to understand that the missing page is a page that moves, we would like to redirect our users to the new url. This is important for website statistics and for social media. But search engines don’t like redirections. The legitimate way to perform a redirection is to use the 301 status code.

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        ...
        If(MustRedirect) Then
            Response.StatusCode = 301
            Response.Redirect("new-url-for-page.aspx")
        End If
    End Sub

The above code makes your page redirects to be search engine friendly. But if you check the response status of the last page it would be 200. To actually check that the 301 code is applied correctly you could either check in websites that offer this analysis, e.g. Redirect Checker. Also, you could check out the Live HTTP Headers firefox plugin.

Filed Under: ASP.NET, IIS Tagged With: ASP.NET MVC, ASP.NET Web Forms, Firefox addons, SEO, Url rewriting

Reader Interactions

Comments

  1. Samuel says

    February 23, 2017 at 15:43

    very help full article and it works good.

    Reply

Leave a Reply to Samuel Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Categories

  • .NET Development
  • ASP.NET
  • Databases
  • Fun
  • IIS
  • JavaScript
  • Web Development

Tags

.NET Core Android ANTLR ASP.NET Ajax ASP.NET Core ASP.NET MVC ASP.NET Web Forms AWS Bouncy Castle Chartjs cli Client info detection Comic Continuous integration CSS Data backup Date handling Firebase Firefox addons Github HigLabo HTML5 Image manipulation jQuery JWT MySQL Nodejs Nuget OAuth Objectionjs OOP openssl Oracle ORM PHP Regular expressions SEO Social media SQL SQL Server UI/UX Url rewriting Videos Visual Studio Web design

Meta

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org

Secondary Sidebar

Archives

  • July 2020
  • March 2020
  • August 2019
  • December 2018
  • November 2018
  • February 2018
  • August 2016
  • June 2016
  • May 2016
  • February 2016
  • January 2016
  • August 2015
  • July 2015
  • October 2014
  • July 2014
  • November 2013
  • April 2013
  • February 2013
  • January 2013
  • December 2012
  • November 2012
  • August 2012
  • May 2012
  • February 2012
  • December 2011
  • October 2011
  • September 2011
  • August 2011
  • July 2011
  • May 2011
  • April 2011
  • March 2011
  • February 2011
  • January 2011
  • December 2010
  • November 2010
  • October 2010
  • September 2010
  • August 2010
  • July 2010

Footer

Recent Posts

  • Anatomy of an Objection.js model
  • Check your RSA private and public keys
  • Round functions on the Nth digit
  • Send FCM Notifications in C#
  • Jwt Manager
  • Things around the web #5
  • Query JSON data as relational in MySQL
  • Create and sign JWT token with RS256 using the private key
  • Drop all database objects in Oracle
  • Create and deploy a Nuget package

Latest tweets

  • Geekiness Intensifies.. NASA used Three.js to render a real-time simulation of this week's NASA rover landing on M… https://t.co/orgkXnYj9O February 19, 2021 18:12
  • Things I Wished More Developers Knew About Databases https://t.co/h4gfq6NJgo #softwaredevelopment #databases May 3, 2020 12:52
  • How a Few Lines of Code Broke Lots of Packages https://t.co/p7ZSiLY5ca #javascript May 3, 2020 12:48
  • Can someone steal my IP address and use it as their own? https://t.co/HoQ7Z3BG69 January 24, 2020 13:27
  • Organizational complexity is the best predictor of bugs in a software module https://t.co/aUYn9hD4oa #softwaredevelopment January 13, 2020 08:24
  • http://twitter.com/codingstill

Misc Links

  • George Liatsos Blog
  • Plethora Themes
  • C# / VB Converter
  • Higlabo: .NET library for mail, DropBox, Twitter & more

Connect with me

  • GitHub
  • LinkedIn
  • RSS
  • Twitter
  • Stack Overflow

Copyright © 2021 · eleven40 Pro on Genesis Framework · WordPress · Log in