Monday, October 13, 2008

Code for 301 Redirect using c#/ASP.NET

if you want to do a 301 permanent redirect using asp.net and c# copy and paste the code in ur global.asax file.

protected void Application_BeginRequest(object sender, EventArgs e)
{
string newpath = "";
string newpath1 = "";


string sOldPath = HttpContext.Current.Request.Url.ToString();
sOldPath =sOldPath.Substring(sOldPath.LastIndexOf("/")+1);
//
if(sOldPath=="Online.aspx")
{
newpath = "http://www.the.com/online-degree.aspx";
HttpApplication app = sender as HttpApplication;

app.Context.Response.RedirectLocation = newpath.ToString();
app.Context.Response.StatusCode = 301;
app.Context.Response.End();

}}

if you have any questions on doing a 301 redirect please let me know.

No comments: