How to 301 redirect in ASP.NET 4.0?

Version 4 of .NET actually has an improved function for single page implementation - the redirectpermanent.

Response.RedirectPermanent(NEW_URL);


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