title: How to 301 redirect in ASP.NET 4.0?
body: <p>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.</p>
  
  <p>I want to have <a href="http://website.net" rel="noreferrer">http://website.net</a> as my main url &amp; want to have a permanent URL redirect if someone types in <a href="http://www.website.net" rel="noreferrer">http://www.website.net</a>.</p>
  
  <p>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.</p>
  
  <pre><code>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"));
      }
  
  }
  </code></pre>