posted 9/1/2010 by Vivek Thakur
For efficient SEO, it is better that each URL should render unique data. So that means URLs having duplicate content, like http://mysite.com, http://www.mysite.com and http://www.mysite.com/default.aspx should be redirected to only a single URL having the same content. Scott Mitchell has a really nice article on this topic. But there is a catch!
AFAIK, the code below (from the article) to redirect site.com/default.aspx to site.com/ will not work if there is a sub-directory involved:
if (request.RawUrl.Equals("/default.aspx")){newUrl = string.Format("{0}://{1}{2}", request.Url.Scheme, request.Url.Authority, request.RawUrl.Remove(request.RawUrl.LastIndexOf("/default.aspx", StringComparison.OrdinalIgnoreCase))); context.Response.Status = "301 moved permanently"; context.Response.AddHeader("Location", newUrl);}
It looks like a bug in ASP.NET: the Request.RawUrl should NOT contain "/default.aspx" when the URL does not have that extension. I tested the above code and it works fine without a sub directory, but if the default.aspx page is under a directory, the Request.RawUrl object fails to get rid of default.aspx and hence goes in an infinite loop. I would like to know what you think about this issue. If it is indeed a bug, then the only option is to use 301-redirect using IIS rewrite rules as mentioned in Scotts article.
**Update**
The same issue is highlighted here:
http://stackoverflow.com/questions/3618022/possible-bug-issue-in-asp-net-3-5-related-to-request-rawurl-property
In one of the posted replies, a nice solution could be to use Request.ServerVariables["CACHE_URL"], but even that depends on the version of IIS and is not very reliable. So the only way is to use IIS Url rewrites.
What kind of email newsletter would you prefer to receive from CodeAsp.Net?18