0

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?

IrishChieftain
  • 15,108
  • 7
  • 50
  • 91

1 Answers1

1

How would I change this code to cater for going from "www" to the "non-www" version?

void Application_BeginRequest(object sender, EventArgs e)
{
    if (!Request.Url.Authority.StartsWith("www"))
        return;

    var url = string.Format("{0}://{1}{2}",
        Request.Url.Scheme,
        Request.Url.Authority,
        Request.Url.PathAndQuery);

        Response.RedirectPermanent(url, true);
}

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?

The Request.Url.Scheme (that is, http or https) is being passed through from the current request, therefore this does not handle that detail.

MVC has a RequireHttps attribute that could be registered as a global filter to handle that redirect. Or you could make a custom one if that doesn't meet your needs.

Canonical tag is for when you have duplicate content on multiple URLs. A 301 redirect avoids that situation, so it is not needed in this case.

NOTES:

301 redirects are cached by most browsers. So, if you have this setup without adding a no-cache header to the response, any browsers that have hit your current configuration will ignore the new configuration. Essentially, unless you find some way to cache bust this situation, these unfortunate users will be in an infinite redirect loop.

301 redirects are not honored by all browsers (although, to the best of my knowledge they are honored by all search engines). See this answer on how to setup a user-friendly 301 redirect for those unfortunate users whose browser doesn't land where you want it to.

NightOwl888
  • 55,572
  • 24
  • 139
  • 212
  • Great answer, but I don't see how to change my existing code in the reverse situation: to direct all "www" to the "non-www" version? – IrishChieftain Jan 27 '18 at 18:30
  • The above code should do the trick. There are a couple of subtle changes 1) `!` before the `www` string check 2) Removed the `www` from the URL that is built. – NightOwl888 Jan 27 '18 at 18:38
  • Aha! Thanks a million, I guessed that the Request.Url.Scheme would not handle it. – IrishChieftain Jan 27 '18 at 18:39