301 redirect with asp.net
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