Wednesday, November 30, 2005

301 redirect with asp.net

Something simple and very common with most SEO projects is the 301 redirect. In the past a company would buy as many domains as they could and point them all to the same site. This gave them better odds of being found as well as making sure someone didn’t get a similar domain name and ranking well for your company. Then the search engines got wise to this trick and started knocking people down in the rankings for duplicate content. There were now two options, release the extra domain names or do a redirect to the one domain name you wanted as your main one. There are two types of domain names one is a 301 redirect and one is a 302 redirect. The 302 is used for moving a resource temporarily it sort of says “hi im over here now but I will be back sometime in the future” this causes the search engines to keep the original url in the index. The other type of redirect is a 301 this says “I am no longer at that address I am now at this new address so don’t look there for me any more” this is the main way you tell search engines you want to have many domain names pointing to one site but only have the content indexed under one of the names. In the linix world and the classic asp world this is very easy. You can just change the .httaccess file. It is also easy on windows 2k3 if you have direct access to the server and are not running in a cluster environment. The sad part of my life is I live n a world where developers do not touch the production boxes and network ops does not make changes to IIS for us on the production boxes for something as trivial in there mind as a 301 redirect. So I had to solve it in code. The basic part of this is simple you just simple need to put in

Response.Status = "301 Moved Permanently";
Response.AddHeader("Location","http://www.redirectdemo.com" + urlPath());

And you are done. In a perfect world this would be all you needed (in a truly perfect world I would have access to a .httaccess file). But I am also running in three different environments also I want to keep the original path typed in by the user so they don’t just end up back at the home page and have to start over. So this is what I built. First I find there requested path and check to make sure there not requesting the default.aspx file (sometimes google will index the root and the default.* file as different pages). If it is default then I set urlPath to “” if not I set urlPath to the requested url. I then check the domain and make sure it isn’t any of the domain names I want to allow at the step I have to include the domain name I want them to land on if not it will stick in a loop of constantly sending the user back to the original domain name. I built this into a switch just because I don’t feel there will ever be more then a few domains I don’t want to redirect on. If this list was going to be large or if I was going to redirect different domains to different places I would put this into an array. This is all built into a user control so I can simply add it to every page on the site and if I ever want to add a domain name to this I simple have to update the user control and it updates everything at once. This is the first piece of code I have ever released to the public but if this helps anyone please feel free to use it all you want. Also if you do use it or find it useful please let me know.

The full code listing



namespace redirectDemo.controls
{
using System;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
///
/// redirect usercontrol. this will redirect all production domains to http://www.redirectdemo.com
///

public class redirect : System.Web.UI.UserControl
{
private void Page_Load(object sender, System.EventArgs e)
{
checkDomain();
}
public string urlPath()
{
string redirectPath;
redirectPath = HttpContext.Current.Request.RawUrl.ToString();
if (redirectPath == "/default.aspx")
{
redirectPath = "";
}
return redirectPath;
}
private void checkDomain()
{
switch (Application["domain"].ToString())
{
case "localhost":
case "site.dev.server":
case "site.stag.serv":
case "www.redirectdemo.com":
break;
default:
Response.Status = "301 Moved Permanently";
Response.AddHeader("Location","http://www.redirectdemo.com" + urlPath());
break;
}
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///

private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home