ASP.NET MVC 2 Redirect from non-www to www

1.4k Views Asked by At

I found solutions for redirecting from www to non-www but not vice versa. Is there any "simple" solution to do this? And what is the difference between www or non-www? Which one should I use? Is it only because of the shorter version? My reason for using only one version is because of SEO.

2

There are 2 best solutions below

0
On

Here is some background info that you might find useful: http://www.mattcutts.com/blog/seo-advice-url-canonicalization/

http://mydomain.com is actually a separate domain from http://www.mydomain.com. Thus is if you have the same content on both domains they will be treated as duplicates of each other.

I handle my redirects by setting up two sites in IIS and then doing a permanent 301 redirect from one to the other. You can also do this in code if you have to though it's not my preferred method. See one option at: http://www.eworldui.net/blog/post/2008/04/25/ASPNET-MVC-Legacy-Url-Routing.aspx

0
On

You can use this code for any asp.net application at Global.asax file:

void Application_BeginRequest(object sender, EventArgs e)
{
   string FromHomeURL = http://yourdomain.com;    
   string ToHomeURL = http://www.yourdomain.com;

   if(HttpContext.Current.Request.Url.ToString().ToLower().Contains(FromHomeURL))
   {
       HttpContext.Current.Response.Status = "301 Moved Permanently";
       HttpContext.Current.Response.AddHeader("Location",
       Request.Url.ToString().ToLower().Replace(FromHomeURL, ToHomeURL));
   }
}

I don't think there will be difference between using or not but better use only one version. Also there is a setting for preferred domain at Google Webmaster Tools. I prefer using non-www. So do stackoverflow.com :)