I have the following code to redirect all requests for "non-www" versions of my site to go instead to the "www" version and the redirect is working. I updated the settings in Google Webmaster Tools accordingly. I obtained this code from an existing SO question.
void Application_BeginRequest(object sender, EventArgs e)
{
if (Request.Url.Authority.StartsWith("www"))
return;
var url = string.Format("{0}://www.{1}{2}",
Request.Url.Scheme,
Request.Url.Authority,
Request.Url.PathAndQuery);
Response.RedirectPermanent(url, true);
}
How would I change this code to cater for going from "www" to the "non-www" version? What's confusing me is that all sites start with "http://". Secondly, how would I do this if I migrated my site to "https://"? Would a canonical link take care of this or would I again have to change the above code for that?