I am trying to implement URL redirect for the website rather than doing it page by page. I want to do it in the global.asax file. Below is the code i have defined.
I want to have http://website.net as my main url & want to have a permanent URL redirect if someone types in http://www.website.net.
Unfortunately it is not working for the live website. Can anyone point out the problem in the code. The code doesn't generate any error.
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
if (HttpContext.Current.Request.Url.ToString().ToLower().Contains("http://website.net"))
{
HttpContext.Current.Response.Status = "301 Moved Permanently";
HttpContext.Current.Response.AddHeader("Location", Request.Url.ToString().ToLower().Replace("http://website.net", "http://www.website.net"));
}
}
RedirectPermanent here. This will automatically issue the 301 moved permanently status code and redirect to the target page. A permanent redirect status code tells a search engine to update their cache and reassign the old url to the new url.
A 301 redirect is a permanent redirect that passes full link equity (ranking power) to the redirected page. 301 refers to the HTTP status code for this type of redirect. In most instances, the 301 redirect is the best method for implementing redirects on a website.
Add the 301 redirection code htaccess” file and click “Edit”. Alternatively, you can right-click on the file and select the “Edit” option there. To move on to the next step, click “Edit” in the dialogue box that appears. Make sure to replace the example URLs with the URLs of the actual pages you want to redirect.
Main problem: Your're doing the above stuff in Application_Start
- which is only executed once. You should hook up with each request. Try this:
void Application_BeginRequest(object sender, EventArgs e)
{
// Code that runs on every request
if (HttpContext.Current.Request.Url.ToString().ToLower().Contains("http://website.net"))
{
HttpContext.Current.Response.Status = "301 Moved Permanently";
HttpContext.Current.Response.AddHeader("Location", Request.Url.ToString().ToLower().Replace("http://website.net", "http://www.website.net"));
}
}
An even better approach would be to use URL rewriting, which can be configured from within Web.Config
:
Microsoft rewriting module - Force www on url Or remove www from url
If using IIS 7 or higher, the simplest solution is to use the httpRedirect element in your web.config.
<httpRedirect enabled="true" exactDestination="true" httpResponseStatus="Permanent">
<add wildcard="/MyOldAspFile.aspx" destination="/MyNewFile.aspx" />
<add wildcard="/MyOldHtmlFile.html" destination="/MyNewFile.aspx" />
</httpRedirect>
This method is very powerful, for example if you have changed the domain but the pages are the same, you have just to add:
<system.webServer>
<httpRedirect enabled="true" childOnly="true" destination="http://www.mynewdomain.com/" />
</system.webServer>
I wrote a small article here: ASP.NET 301 permanent redirects: the best solution
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With