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

I found solutions to redirect from www to non-www, but not vice versa. Is there a โ€œsimpleโ€ solution for this? And what is the difference between www or non-www? Which one should I use? Is it just because of the shorter version? My reason is to use only one version due to SEO.

+3
source share
2 answers

Here is some background information that may come in handy: http://www.mattcutts.com/blog/seo-advice-url-canonicalization/

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

I process my redirects by setting up two sites in IIS, and then perform 301 continuous redirects from one to the other. You can also do this in code if you need to, although this is not my preferred method. See One Option: http://www.eworldui.net/blog/post/2008/04/25/ASPNET-MVC-Legacy-Url-Routing.aspx

+3
source

You can use this code for any asp.net application in the 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));
   }
}

, , . Google -. -www. /fooobar.com/...:)

+2

Source: https://habr.com/ru/post/1792987/


All Articles