SEO Friendly Custom Error Pages
404 - missing page - search engine will remove page from cache
500 - error in code - search engine will ignore page not updating it's cache for that url
302 - temporary new location (redirect) - search engine will update the cache of the original url with the contents of the new location
200 - page loaded ok - search engine will update it's cache for that url
Now ASP.NET 3.5 did introduce the ability to not perform a redirect and instead simply serve up of the contents of the error page for the requested url if you specify the attribute redirectMode="ResponseRewrite". The problem is that this causes the status code to be changed back to 200, which is not we want. The solution which only works on IIS7 is putting this in your web.config:
<system.webServer> <httpErrors existingResponse="Replace"> <remove statusCode="500" subStatusCode="-1" /> <remove statusCode="404" subStatusCode="-1" /> <error statusCode="404" prefixLanguageFilePath="" path="404.htm" responseMode="File" /> <error statusCode="500" prefixLanguageFilePath="" path="500.htm" responseMode="File" /> </httpErrors> </system.webServer>
One of the configurations to be aware of is existingResponse="Replace" because without this set the default error page asp.net generates will be served up instead. This only works for static html files so if you want your error pages to be dynamic aspx files instead you can choose to execute a url instead by specifying responseMode="ExecuteURL" (htm files are better since they will still work if you are uploading the site or when there's an error in the Master page your error page inherits from). Unfortunately this sets the status code back to 200, so in the code behind for your error page you can call:
<error statusCode="500" prefixLanguageFilePath="" path="/500.aspx" responseMode="ExecuteURL"/>
protected void Page_PreRender(object sender, EventArgs e) { //this fixes the ResolveClientUrl paths if page with error is in a different folder to the error page HttpContext.Current.RewritePath("~/"); Page.Header.Controls.AddAt(0, new LiteralControl("<base href='" + Request.Url.Scheme + "://" + Request.Url.Authority + ResolveUrl("~") + "'/>")); Response.StatusCode = 500; }
This could also be used with customErrors set to ResponseRewrite with Response.TrySkipIisCustomErrors = true; in the code behind, but for 404's that don't end in an asp.net extension like .aspx say a typo like .asxp it won't serve up your friendly 404 error page, but if you use httpErrors it will.
To observe what status code is being served up I'm using Firebug on Firefox and looking under the net tab.
No comments:
Post a Comment
Thank You for Your Comments. We will get back to you soon.