About Me

"I love being the underdog"

Anthony DeSalvo

Dad, Husband, Roller Derby Widow, Triathlete, Web Developer and RCRD Volunteer!

301 Moved Permanently - Redirecting URLs with C# / ASP.NET

by adesalvo 26. May 2007 01:45

I recently attended the 7 Secrets to High Google Rankings seminar at the RMSC. The seminar was sponsored by the AMA Rochester chapter and was presented by Stephan Spencer, President of Netconcepts.

Stephen gave an excellent presentation of seven SEO concepts to better optimize a web site. One of the ones that really stuck out for me was the 301 Redirect. A 301 Redirect is a search engine friendly way to move pages without forfeiting hard earned search engine placement. Redirecting your Domain foo.com to www.foo.com is also a common use for a 301 redirection. What makes a 301 important is it merges all your searched and indexed pages into one domain, thus increasing your PageRank.

After the seminar I went back and tested one of the corporate sites I manage using a 301 redirect checker only to find out that I was not using this SEO method.

Below is the code I used to implement the 301 Redirect. I know there are many ways to do this, DNS, URL_REWRITE, etc, but the easiest most controlling method was to use code. This would allow me to manage from a CMS what domains will be redirected and where. There is a future business requirement to internationalize the site using different languages and domains. Another requirement was to maintain the state of the Query String. Below is the c# code I placed on a web control that is present on every page of the site. A more optimal way to implement this would be to extend the System.Web.UI.UserControl on every page and overide the page_load event to containg the below code.

    
protected void Page_Load(object sender, EventArgs e)
  {

      /* 301 redirect with query string support.*/

      string c_absUri = Request.Url.DnsSafeHost;
      string c_absPath = Request.Url.AbsolutePath;

      // shorthand to set query string value
      string c_queryString = Request.QueryString.ToString().Length > 0 
             ? Request.QueryString.ToString() : String.Empty;

      //Only Redirect if Doain is not int he NoRedirect List
      if (!isNoRedirectUrl(c_absUri))
      {
          // eventualy I would create a hash to lookup 
          //the redirect instead of hardcoding it
          string newUrl = "http://www.foo.com" + c_absPath;

          //is querystring is not empty Append to redirect url
          if (!c_queryString.Equals(string.Empty))
          {
              //add query string
              newUrl += "?" + c_queryString;
          }

          Response.Status = "301 Moved Permanently";
          Response.AddHeader("Location",newUrl);
      }

  }

  public static bool isNoRedirectUrl(string Url)
  {
      // This could be an externally managed list - CMS maybe?
      string[,] a_noRedirectUrl = new string[,] {
          
              {"ext.foo.com"},
              {"dev.foo.com"},  
              {"beta.foo.com"},
              {"localhost"},                                        
              {"foo.co.uk"}                           
          };

      bool isNoRedirectUrl = false;
      foreach (string s in a_noRedirectUrl)
      {
          if (Url.Contains(s))
          {
              isNoRedirectUrl = true;
          }
      }

      return isNoRedirectUrl;

  }

Tags: , , ,

Tech