301 domain based Redirect not working in global.asax file, any ideas?

301 domain based Redirect not working in global.asax file, any ideas?

This question is not answered

I have multiple domains going to one CS install for  tracking & marketing purposes but we want all of the domains to go to just the one domain for SEO purposes.

This is what I'm trying to use:

<script language="C#" runat="server">
 
 private void Page_Load(object sender, System.EventArgs e)
        {
if (HttpContext.Current.Request.Url.ToString().ToLower().Contains( "http://olddomain.tld"))
{
    HttpContext.Current.Response.Status = "301 Moved Permanently";
    HttpContext.Current.Response.AddHeader("Location", Request.Url.ToString().ToLower().Replace( "http://olddomain.tld", "http://newdomain.tld"));
}
}
</script>

Verified Answer
  • I have done this before using an HttpModule and the Application_BeginRequest method. You should be able to use Application_BeginRequest in Global.asax as well. I don't think that Page_Load is what you want.

  • Ah, yeah your right not sure why I used on load event...

    Any ideas on how to make it so it doesn't forward to the domain/default.aspx, I'm trying to keep the SEO on the main domain/

    This works:

    <script language="C#" runat="server">
     
    protected void Application_BeginRequest(Object sender, EventArgs e)
            {

    if (HttpContext.Current.Request.Url.ToString().ToLower().Contains("http://olddomain"))
    {
        HttpContext.Current.Response.Status = "301 Moved Permanently";
        HttpContext.Current.Response.AddHeader("Location", Request.Url.ToString().ToLower().Replace("http://olddomain","http://newdomain"));
    }

    }
    </script>

    However is forwards all to newdomain/default.aspx and then google index's the domain/default.aspx page instead of the domain itself.

All Replies
  • I have done this before using an HttpModule and the Application_BeginRequest method. You should be able to use Application_BeginRequest in Global.asax as well. I don't think that Page_Load is what you want.

  • Ah, yeah your right not sure why I used on load event...

    Any ideas on how to make it so it doesn't forward to the domain/default.aspx, I'm trying to keep the SEO on the main domain/

    This works:

    <script language="C#" runat="server">
     
    protected void Application_BeginRequest(Object sender, EventArgs e)
            {

    if (HttpContext.Current.Request.Url.ToString().ToLower().Contains("http://olddomain"))
    {
        HttpContext.Current.Response.Status = "301 Moved Permanently";
        HttpContext.Current.Response.AddHeader("Location", Request.Url.ToString().ToLower().Replace("http://olddomain","http://newdomain"));
    }

    }
    </script>

    However is forwards all to newdomain/default.aspx and then google index's the domain/default.aspx page instead of the domain itself.