Click here to Skip to main content
13,350,822 members (56,589 online)
Click here to Skip to main content
Add your own
alternative version


18 bookmarked
Posted 17 Sep 2010

Handling 301 Redirects in ASP.NET 3.5

, 8 Mar 2011
Rate this:
Please Sign up or sign in to vote.
Handling 301 Redirects in ASP.NET form basic page control to HHTP modules


You are putting a new site together and you have lots of new pages and lots of old pages that will no longer exist, it might be a good idea to tell links from external sources and search engines, robots, etc... that the pages have moved. Supplying incoming requests with some notification of page moves or page replacements is handled with "301 Moved Permanently".

There are a few instances where this is a good idea:

  • Complete redesign of a site
  • Single Page move
  • Move from static site htm to aspx
  • You don't want to lose search rankings
  • You can't define all your external links

Background : What a "301 Moved Permanently" is states:

10.3.2 301 Moved Permanently

The requested resource has been assigned a new permanent URI and any future references to this resource SHOULD use one of the returned URIs. Clients with link editing capabilities ought to automatically re-link references to the Request-URI to one or more of the new references returned by the server, where possible. This response is cacheable unless indicated otherwise.

The new permanent URI SHOULD be given by the Location field in the response. Unless the request method was HEAD, the entity of the response SHOULD contain a short hypertext note with a hyperlink to the new URI(s).

If the 301 status code is received in response to a request other than GET or HEAD, the user agent MUST NOT automatically redirect the request unless it can be confirmed by the user, since this might change the conditions under which the request was issued.

Basic Redirects

If an old page still exists, then you can add code to the Page_Load event which will look like the code below:

protected void Page_Load(object sender, EventArgs e)
            // Flag Page has moved
            Response.Status = "301 Moved Permanently";
            // Ask Browser to move to new page
            Response.AddHeader("Location", "NewPage_Redirect1.aspx");

The above code has advantages and disadvantages:


  • Quick
  • Easy


  • You need to maintain that page from now on in your application
  • Does not work with downloadable content such as picture, PDFs, media, etc..
  • You still have to contain old folder structure in your new site.
  • Does not work with directories/folders redirects.
  • Does not work if moving site from Java, Apache to IIS

Expanding from Basic Redirects

So we need something that will give us a clean site and be able to handle a mix of incoming requests and move them to the right page or folder. Also dealing with non web application files such as pdf and Word documents would be good as well.

The best way to achieve this is to use a custom HTTP Module which will deal with requests and point the client browser to the correct page.

Creating a Custom HHTP Module for Redirect

First, you needed to create a class that inherits from the interface IHttpModule and implement two members as shown below.

Note: We will fill in the members later on

public class HttpRedirect : IHttpModule
       public void Init(HttpApplication context)
           throw new NotImplementedException();

       public void Dispose()
           throw new NotImplementedException();

Create Entry in Web.Config for the Redirect Class

Below is an entry for the Redirect classes called "HttpRedirect" which will fire on an incoming request.

        <add name="Redirects" type="Redirects301.HttpRedirect,Redirects301"/>
        <add name="ScriptModule" preCondition="managedHandler" 

        type="System.Web.Handlers.ScriptModule, System.Web.Extensions, 
        Version=, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>

Redirect List

I had about 100 or so redirects and some might change or others might be added later so I added them in an XML file that I could load and changes if required without rebuilding the application.

So there is an XML file called  Redirect301.xml which holds the redirects.

<?xml version="1.0" encoding="utf-8" ?>
    <Description>Change of Index</Description>
    <Description>Directory </Description>
    <Description>PDF Redirect</Description>

The contents of this have the following function:

  • Description: Does nothing in Code, purely for describing the Redirect
  • OldPage: What the old page was, this will be the incoming request that you will look out for
  • NewPage: Target Location for the OldPage request.
  • File: The NewPage is a file download rather than a support format in IIS; I used this for PDFs

Adding Location of this to Web.Config

Simple Application Key as shown below, this is used by the HttpRedirect class to find the redirect XML.

         <add key="RedirectXMLDoc" value="/Redirects301.xml"/>

Redirect Class

Used to store the redirects in a usable format for the redirect. This roughly matches the contents of the Redirect301.xml file.

namespace Redirects301
        public class Redirect
            public string OldPage { get; set; }
            public string NewPage { get; set; }
            public bool TransferFileType { get; set; }

Business End: HttpRedirect Class

This is the full list of the HttpRedirect class.

using System;
using System.Collections.Generic;
using System.Web;
using System.Linq;
using System.Web.Configuration;
using System.Xml.Linq;

namespace letsure
    public class HttpRedirect : IHttpModule
        public void Init(HttpApplication ap)
            ap.BeginRequest += new EventHandler(ap_BeginRequest);

        /// <summary>
        /// Filename of the Redirect301 xml document
        /// </summary>
        static string Xmldoc
                string filename = WebConfigurationManager.AppSettings["RedirectXMLDoc"];
                return HttpContext.Current.Server.MapPath(filename);

        static void ap_BeginRequest(object sender, EventArgs e)
                // Get current HttpApplication Request
                HttpApplication hxa = (HttpApplication) sender;

                //Get Incoming Request for page.
                string incomingText = hxa.Context.Request.Url.AbsolutePath.Trim();

                // This deals with ISS issues regarding it does not 
                // service non-existant pages
                // so create a custom 404.aspx page change ISS to attach 
                // custom 404 to this page
                // and this code will handle this.
                if (hxa.Request.Url.LocalPath.Contains("404.aspx"))
                    incomingText = hxa.Request.Url.Query.ToLower();
                    //script away the errorpath from query string to get the original
                    //requested url
                    incomingText = incomingText.Replace("?aspxerrorpath=", string.Empty);

                    // Fix for SERVER IIS6

                    incomingText = incomingText.Replace("?404;", string.Empty);
                    string schemeHost = hxa.Request.Url.Scheme + "://" 
					+ hxa.Request.Url.Host;
                    incomingText = incomingText.Replace(schemeHost, string.Empty);

                    // Fix for Ports numbers on ports that are not default
                    string portvalue = ":" + hxa.Request.Url.Port;
                    incomingText = incomingText.Replace( portvalue, "");

                // Try and Check for redirect

                    // Check if in list
                    var results = from r in Redirects()
                                  where incomingText.ToLower() == r.OldPage.ToLower()
                                  select r;

                    //Redirect if required.
                    if (results.Count() > 0)
                        //Select First Redirect if we have multiple matches
                        Redirect r = results.First();

                        // Add Moved Permanently 301 to response
                        hxa.Response.Status = "301 Moved Permanently";

                        //Test the Response is an attachment type ie not hmtl, 
		      //aspx, .htm, html, etc....
                        if (r.TransferFileType)
				"attachment; filename=filename");

                        // Go to new page
                        hxa.Response.AddHeader("Location", r.NewPage);

                catch (Exception)
                    //Redirect to page not found if we have an error
                    //hxa.Response.AddHeader("Location", @"\404.aspx");

        /// <summary>
        /// Dispose; there is nothing to dispose.
        /// </summary>
        public void Dispose() { }

        /// <summary>
        /// Return a collection of Redirect
        /// </summary>
        /// <returns></returns>
        private static IEnumerable<Redirect> Redirects()
            //Get Collection of Redirects
            var results = new List<Redirect>();

            //Get Redirect File XML name for
            XDocument xdocument = GetXmLdocument();

            var redirects = from redirect in xdocument.Descendants("Redirect")
                            select new
                                           oldpage = redirect.Element("OldPage").Value,
                                           newpage = redirect.Element("NewPage").Value,
                                           file = redirect.Element("File").Value
            foreach (var r in  redirects)
                var redirect = new Redirect
                                       NewPage = r.newpage,
                                       OldPage = r.oldpage,
                                       TransferFileType = Convert.ToBoolean(r.file)


            return results;

        /// <summary>
        /// Get Redirect Doc from Cache
        /// </summary>
        /// <returns></returns>
        private static XDocument GetXmLdocument()
            //create empty return doc
            XDocument xmldocument;

            //check if Cache is null; if so add it
            if (HttpRuntime.Cache["RedirectXMLDoc"] == null)
                xmldocument = XDocument.Load(Xmldoc);
                HttpRuntime.Cache["RedirectXMLDoc"] = xmldocument;
                //Get from Cache.
                xmldocument = (XDocument) HttpRuntime.Cache["RedirectXMLDoc"];

            return xmldocument;
<appSettings>  </appSettings>

Points of Interest

Redirecting to a file supplied from the code below as it added a head to tell the client browser to expect a file. I could have tested the extension of the incoming request and decided if it was file or note, but adding something into the HTML is simpler and gives a little more control.

//Test the Response is an attachment type ie not hmtl, aspx, .htm, html, etc....
if (r.TransferFileType)
    hxa.Response.AddHeader("content-disposition", "attachment; filename=filename");


Some of you may have noticed a possible performance issue?

The issue is that for every request the Redirect XML document will have to be opened, read and searched. This is not ideal by any means and this should be cached as the application level and due to popular demand I have added this in.

Folder Redirection

This code does not redirect requested for folders; that is if you have a link for  /old/site.aspx and you want to go to /new/site.aspx. Placing a rule oldfile=/old newfile=/new will not redirect to page correctly. But if your incoming requests is /old, it will redirect to /new on a folder level as it will pickup your default page for that folder.


Test, Test again and then get someone else to Test again. Don't assume my code or anyone else's works the way it is supposed to, this worked for me but you might have problems.


I used this code on the following without any issues with:

  • VS 2008
  • .NET FrameWork 3.5
  • IIS 6-7


I don't think you need any, unless you can't copy or paste.


  • Version 1.000.001 17-Sept-2010
    • Initial release
  • Version 1.000.002 04-Mar-2011
    • Cache added
    • Fixes for IIS 5/6/7


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Frank Kerrigan
Software Developer (Senior)
United Kingdom United Kingdom
Frank Kerrigan

Currently developing Insurance systems with SQL Server, ASP.NET, C#, ADO for a company in Glasgow Scotland. Very keen on OOP and NUNIT testing. Been in IT forever (20 years) in mix of development and supporting applications / servers. Worked for companies big and small and enjoyed both.

Developed in (newest first) : C#, Progress 4GL, ASP.NET, SQL TSQL, HTML, VB.NET, ASP, VB, VBscript, JavaScript, Oracle PSQL, perl, Access v1-2000, sybase/informi, Pic Controllers, 6502 (ask your dad).

Msc .Net Development Evenings
MCAD Passed
MCP C# ASP.NET Web Applications
MCP SQL Server 2000
HND Computing
OND / HNC Electrical Engineering,

You may also be interested in...


Comments and Discussions

BugBug in web.config configuration for this code Pin
Abhijit Talukdar28-Apr-13 23:01
memberAbhijit Talukdar28-Apr-13 23:01 
Question301 module working only on local host Pin
denford26-Oct-12 5:09
memberdenford26-Oct-12 5:09 
AnswerRe: 301 module working only on local host Pin
Frank Kerrigan26-Oct-12 5:47
memberFrank Kerrigan26-Oct-12 5:47 
GeneralRe: 301 module working only on local host Pin
denford30-Oct-12 1:51
memberdenford30-Oct-12 1:51 
Member 917112817-Sep-12 20:42
memberMember 917112817-Sep-12 20:42 
Questiondatagrid and redirect Pin
J Ogbonnaya11-Oct-11 5:58
memberJ Ogbonnaya11-Oct-11 5:58 
AnswerRe: datagrid and redirect Pin
Frank Kerrigan11-Oct-11 6:10
memberFrank Kerrigan11-Oct-11 6:10 
GeneralCaching in application level Pin
Ilias Antoniadis28-Feb-11 2:51
memberIlias Antoniadis28-Feb-11 2:51 
GeneralRe: Caching in application level Pin
Frank Kerrigan4-Mar-11 10:53
memberFrank Kerrigan4-Mar-11 10:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.180111.1 | Last Updated 8 Mar 2011
Article Copyright 2010 by Frank Kerrigan
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid