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(“http://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

 

Both comments and pings are currently closed.

Comments are closed.

A Hotels in Malta Theme. Designed by Malta Hotel and Malta Hotels