Redirect To A Base URL In Umbraco


I had a website with multiple bindings eg

Using IIS, I wanted to force requests to the second two domains to redirect to

I asked how to do this on ServerFault and got this answer. Although this would work, I decided to implement it as a Macro which as a developer I find easier. The macro is:

    string url = Request.Url.ToString();
    string base_url = "";
    string server_name = Request.ServerVariables["SERVER_NAME"];

    if (server_name.IndexOf(base_url) != 0)
      url = url.Replace(server_name, base_url);
      Response.Status = "301 Moved Permanently";
      Response.AddHeader("Location", url);