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); } }
Remember Me
Page rendered at Thursday, February 23, 2012 8:16:41 AM (South Africa Standard Time, UTC+02:00)
Disclaimer The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.