Hungry Beast
Hungry Beast

Reputation: 3725

Redirecting Users from Old (Non-Existing) Page to New Page

I have removed the old login page ClientLogin.aspx in my asp.net 4.0 app and have replaced it with Login.aspx. Whenever a user hits the old login page I want the application to automatically redirect the user to the new login page. I thought there was a very simple way to do this in the web.config. I would prefer to not have to keep the old page around and manually redirect the user with Response.Redirect.

Upvotes: 0

Views: 1760

Answers (3)

Khalid Abuhakmeh
Khalid Abuhakmeh

Reputation: 10839

An HttpHandler might be the best thing to do in your case, because you might eventually want to remove it from your application. I would just use the code below as a starting point and modify it to meet your needs. Also make sure you use the Permanent Redirect code so that if this is a public facing site, Google or other search engines realize that it has moved.

   ///
   /// Summary description for Redirect.
   ///
   public class Redirect : IHttpHandler
   {
         public Redirect() {}

         #region IHttpHandler Members

         public void ProcessRequest(HttpContext context)
         {                  
               context.Response.Redirect("Login.aspx");
         }

         public bool IsReusable
         {
               get
               {
                     return true;
               }
         }

         #endregion
   }

inspired by this code at http://jaysonknight.com/blog/archive/2005/03/31/Using-an-HttpHandler-to-Forward-Requests-to-a-New-Domain.aspx

Once you have your HttpHandler written just register it in your web.config and you are ready to go, just make sure to set the path of the handler to your old url. "ClientLogin.aspx".

<httpHandlers>
     <add verb="*" path="/ClientLogin.aspx" type="My.Web.Redirect, My.Web" />
</httpHandlers>

Upvotes: 2

Robert Beaubien
Robert Beaubien

Reputation: 3156

In your Global.asax.vb file add the following code under the Application_BeginRequest event:

    Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
    ' Fires at the beginning of each request
    If Request.Url.AbsoluteUri.ToUpper.Contains("CLIENTLOGIN.ASPX") = True Then
        Response.Redirect("Login.aspx")
    End If
End Sub

or in C# if you like:

        protected void Application_BeginRequest(object sender, EventArgs e)
    {
        if (Request.Url.AbsoluteUri.ToUpper().Contains("CLIENTLOGIN.ASPX") == true)
        {
            Response.Redirect("Login.aspx");
        }
    }

Upvotes: 3

Adam Tuliper
Adam Tuliper

Reputation: 30152

You can do this in IIS - see: http://knowledge.freshpromo.ca/seo-tools/301-redirect.php

Upvotes: 3

Related Questions