Introduction
It is highly recommended to set up a redirect from non-www to www URL. Ideally search engines will most probably catch on to the situation eventually,
in the meantime your site is subject to several different problems that may damage your rankings.
This code can be placed in your
global.asax to ensure
all your visitors are redirected if they don't write www. in the address and redirect http --> https. This code will keep the
URL as is and the querystring.
Using the code
Implement the following code in your global.asax's Application_BeginRequest
:
var context = HttpContext.Current;
if (context == null)
return;
if(Request.IsLocal)
return;
var redirect = false;
var uri = context.Request.Url;
var scheme = uri.GetComponents(UriComponents.Scheme, UriFormat.Unescaped);
var host = uri.GetComponents(UriComponents.Host, UriFormat.Unescaped);
var pathAndQuery = uri.GetComponents(UriComponents.PathAndQuery, UriFormat.Unescaped);
if (!scheme.Equals("https"))
{
scheme = "https";
redirect = true;
}
if (!host.StartsWith("www.", StringComparison.OrdinalIgnoreCase))
{
host = "www." + host;
redirect = true;
}
if (redirect)
{
HttpContext.Current.Response.Status = "301 Moved Permanently";
HttpContext.Current.Response.StatusCode = 301;
HttpContext.Current.Response.AddHeader("Location", scheme + "://" + host + pathAndQuery);
}
To avoid a whole lot of code in your global.asax, you should put this in another class and just call it from global.asax..
My complete implementation
In a common library somewhere in my solution:
public namespace SomeCommonLibrary
{
public static void EnsureSslAndDomain(HttpContext context, bool redirectWww, bool redirectSsl)
{
if (context == null)
return;
var redirect = false;
var uri = context.Request.Url;
var scheme = uri.GetComponents(UriComponents.Scheme, UriFormat.Unescaped);
var host = uri.GetComponents(UriComponents.Host, UriFormat.Unescaped);
var pathAndQuery = uri.GetComponents(UriComponents.PathAndQuery, UriFormat.Unescaped);
if (redirectSsl && !scheme.Equals("https"))
{
scheme = "https";
redirect = true;
}
if (redirectWww && !host.StartsWith("www", StringComparison.OrdinalIgnoreCase))
{
host = "www." + host;
redirect = true;
}
if (redirect)
{
context.Response.Status = "301 Moved Permanently";
context.Response.StatusCode = 301;
context.Response.AddHeader("Location", scheme + "://" + host + pathAndQuery);
}
}
}
And then in my global.asax:
protected void Application_BeginRequest(object sender, EventArgs e)
{
if(!Request.IsLocal)
SomeCommonLibrary.EnsureSslAndDomain(HttpContext.Current, true, true);
}