Redirect all pages to WWW

Here is a simple method of ensure all requests to your application domain include the WWW subdomain prefix.
Place the following code in your Global.asax file.

Note: Uncomment the code in the RedirectToWWW method should you need to prevent redirects while testing locally.

// global.asax
protected void Application_BeginRequest(Object sender, EventArgs e)
{
RedirectToWWW();
}

private static void RedirectToWWW()
{
// prevent adding www. when testing
// if (HttpContext.Current.Request.Url.ToString().ToLower().Contains(“localhost”))
// return;

// Redirect to www.clienturl.com if the user types in clienturl.com.
if (HttpContext.Current.Request.Url != null
&& !String.IsNullOrEmpty(HttpContext.Current.Request.Url.Host)
&& !HttpContext.Current.Request.Url.Host.ToLower().StartsWith(“www”))
{
HttpContext.Current.Response.Redirect(“https://www.” + HttpContext.Current.Request.Url.Host + HttpContext.Current.Request.Url.AbsolutePath);
}
}

Comptia A+ Training, Comptia A+ certification
Best Microsoft MCTS Certification, Microsoft MCITP Training at certkingdom.com

 

Click to rate this post!
[Total: 0 Average: 0]
News Reporter