Software
Website Portfolio
Resume
Wiki
Blog
Contact

Archives

  • October 2010
  • June 2010
  • December 2008
  • February 2008
  • March 2007
  • December 2006
  • November 2006
  • September 2006

Meta

  • Log in

301 Redirect in ASP.NET

Posted on February 15th, 2008

I was working on porting an old page we have, but never quite got around to finishing it entirely, but several days of work had gone into the porting effort. I didn’t want to lose all my hard work so I figured I would check it in, but I also didn’t want people to try and use it before it was truely ready. If you ever experience a similar situation, this code may be of some use, while still retaining some good SEO.

protected override void OnInit(EventArgs e)
{
    bool useNewSignup = Config.Default.GetConfigBool("signup", "useNew", false);        
    if (!useNewSignup)
    {
        string absPath = Request.Url.AbsolutePath;
        string queryString = Request.QueryString.ToString().Length > 0 ? Request.QueryString.ToString() : String.Empty;
 
        if (!String.IsNullOrEmpty(queryString))
        {
            newUrl += "?" + queryString;Response.Status = "301 Moved Permanently";
        }
 
        string newUrl = "/signup.asp" + absPath;
        Response.AddHeader("Location", newUrl);
    }
    base.OnInit(e);
}

Site content and design copyright 2005-2010 bsd.bz