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 http://www.example.com/default.aspx
http://www.example.com redirects to http://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
Most likely the Request.Url is appending default.aspx because that’s the page actually being served at the time (IIS makes this transparent to you because it’s one of the default pages).
When you make your new URL that you’re going to redirect, add another .Replace(‘/default.aspx’, ”) to the end of it. So…