vijay

welcome Netizen

Share Your Knowledge.It is a way to achieve immortality

Thursday, June 21, 2012

Redirect from Old URL to New URL link in asp.net


Here i share the code to redirect old URL link to new URL link, i have explained the case with some example

Description:
Take an example as below as old link used in website
www.dotnetcode.in/details.aspx
Now i have deleted the page details.aspx, instead of details.aspx now i am using new page   user.aspx page URL is www.dotnetcode.in/user.aspx
Normally search engine crawl our old link so it always shows our Old link in search engine result. If the user comes to our website using
Old link we have to redirect the user to the correct page i.e. to new link.

To accomplish the above case we have to add the bit of code in Global.asax file
Add the below sub function in Global.asax file
VB.NET Code:

Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
        If System.Web.HttpContext.Current.Request.RawUrl.Contains("details.aspx") Then
            System.Web.HttpContext.Current.Response.Clear()
            System.Web.HttpContext.Current.Response.Status = "301 Moved Permanently"
            System.Web.HttpContext.Current.Response.AddHeader("Location", "http://www.dotnetcode.in/user.aspx")
            System.Web.HttpContext.Current.Response.End()
        End If
    End Sub
  
  
C# Code:

  If (HttpContext.Current.Request.RawUrl.Contains("details.aspx")) Then
    {
        Response.Clear();
        Response.Status = "301 Moved Permanently";
        Response.AddHeader("Location", "http://www.dotnetcode.in/user.aspx");
        Response.End();
    }

Keep Reading. Post your valuable comments

0 comments:

Post a Comment