301 redirect for discountasp.net domain pointer

Discussion in 'ASP.NET 2.0' started by ronhawker, Jan 23, 2009.

  1. I purchased domain pointer to add to our site for $15 so that our old domain would redirect to the new one that is hosted on discountasp.net. It works properly but is returning a http status code of 302. I have tried setting up the global.aspx to trap the repoint and resetting the header status to 301 so the Google will get the index reset and transfer the link history and prevent duplicate content problems. The global.aspx code is as follows:




    Protected Sub Application_BeginRequest(ByVal sender As Object, ByVal e As System.EventArgs)


    ' Fires at the beginning of each request


    Try


    If InStr(Context.Request.Url.AbsoluteUri, "olddomain") > 0 Then


    Response.Status = "301 Moved Permanently"


    Response.AddHeader("Location", http://www.newdomain.com/)


    Response.Redirect(http://newdomain.com/)


    End If


    This still returns a status code of 302 and then 200 when it lands on the newdomain page.


    I also asked support if they could do this at the server level which is more normal and reliable but they did not understand as I mentioned code such as above and that seemed to shut the issue down for them and they referred the question to the community.
     
  2. Yes, that is the fix. Change the response.redirect to the response.end().
     
  3. I don't know about domain pointers at DASP but I do have an external domain with a DNS A record pointing at my DASP server and I also do a 301 redirect in c# code that works for me. The outline of my app is this: basically I run a CMS system where all content is served from virtual files and the db using a mix of VirtualPathProvider and LINQ technology. This means that I do not actually have a default document at the application root, so I need to 301 redirect all incoming requests. I don't use global.asax for this but have a IHttpModule registered specifically for this purpose.

    I've tested my app redirect code from an external 301 redirect tester on the web and it's working well for me. The code looks like this and it does look very similar to yours however you're welcome to rip it (modify it slightly because you won't need the Segments.Count() check), convert it to VB and give it a shot:

    public class RootRewriterHttpModule : IHttpModule
    {

    #region IHttpModule Members

    public void Init(HttpApplication context)
    {
    context.BeginRequest += new EventHandler(context_BeginRequest);
    }

    public void Dispose()
    {
    }

    #endregion

    private void context_BeginRequest(object sender, EventArgs e)
    {
    HttpContext context = HttpContext.Current;
    HttpRequest request = context.Request;

    if (request.Url.Segments.Count() == 1)
    {
    context.RewritePath('default.aspx');
    context.Response.Status = '301 Moved Permanently';
    context.Response.AddHeader('Location', 'default.aspx');
    context.Response.End();

    }
    }

    }

    I hope this helps,
    Cheers,
    Joe
     

Share This Page