How to redirect permanently to a different domain with ASP.NET?

The redirect forwards the users and respectively the search engine to a new url. The redirections can be achieved in various ways. Commonly used are the HTTP status codes "301 Moved Permanently" and "302 Moved Temporarily".

In regards to the SEO (Search Engine Optimization) it is recommended to use status code 301 to inform the search engine that the previous url will not longer be in use. This url forwarding can be implemented within Global.asax as follows:


protected void Application_PreRequestHandlerExecute(Object sender, EventArgs e)
{
      string url = HttpContext.Current.Request.Url.OriginalString.ToLower();
      if(url.Contains("old-domain.com"))
      {       
        Response.StatusCode = 301;
        Response.AddHeader("Location",
            url.Replace("old-domain.com", "new-domain.com"));       
        Response.End();
      }
}