A few minor changes in Jan's answer made me work for me:
protected void Application_BeginRequest(Object sender, EventArgs e)
{
string currentUrl = HttpContext.Current.Request.Url.ToString().ToLower();
if (currentUrl.StartsWith("http://mydomain"))
{
Response.Status = "301 Moved Permanently";
Response.AddHeader("Location", currentUrl.Replace("http://mydomain", "http://www.mydomain"));
Response.End();
}
}
The change was to use the BeginRequest event and set currentUrl to HttpContext.Current.Request.Url instead of HttpContext.Current.Request.Path. Cm.:
http://www.mycsharpcorner.com/Post.aspx?postID=40
source
share