I implemented a following code in Global.asax file of my web application.
void Application_BeginRequest()
{
string rule = ConfigurationManager.AppSettings.Get("WwwRule");
HttpContext context = HttpContext.Current;
if (context.Request.HttpMethod != "GET" || context.Request.IsLocal)
{
return;
}
if (context.Request.PhysicalPath.EndsWith(".aspx", StringComparison.OrdinalIgnoreCase))
{
string url = context.Request.Url.ToString();
if (!url.Contains("://www.") && rule == "add")
{
string url = context.Request.Url.ToString().Replace("://", "://www.");
context.Response.Redirect(url);
}
}
}
When I am running above code it works as follows
example.com redirects to www.example.com/default.aspx
www.example.com redirects to www.example.com
http://www.example.com/ redirects to http://www.example.com/
last two conditions works very well. But the first condition did'nt works well because its adding "default.aspx" in the URL which I am not intrested in.
Can anyone please tell me how to make it as below
example.com should redirects to http://www.example.com
Thanks