Click here to Skip to main content
15,885,216 members
Articles / Web Development / ASP.NET

Redirector Module

Rate me:
Please Sign up or sign in to vote.
4.68/5 (13 votes)
19 Jan 2010CPOL2 min read 36.1K   35   8
Build an ASP.NET HttpModule to have normalized URLs, and avoid duplicate content for a SEO friendly website.

Contents

Introduction

In this article, I will try to show you how to create an HttpModule to handle requests and redirect (HTTP status codes 301 and 302) them to another URL, in order to have normalized URLs and avoid duplicate content.

This article is not directly related to and does not cover URL rewriting nor ASP.NET Routing.

Background

URL normalization or URL canonicalization is the process by which URLs are modified and standardized in a consistent manner. The goal of the normalization process is to transform a URL into a normalized URL so it is possible to determine if two syntactically different URLs are equivalent.

For Search Engine Optimization, it is very important to have canonical URLs in order to avoid duplicate content. You can read further about this subject on this SEO advice from Google's Matt Cutts: mattcutts.com/blog/seo-advice-Url-canonicalization/.

There are several types of normalization that may be performed. Here is a list of the most commonly used in web projects:

  • Removing "www" as the first domain label
  • http://www.example.com/ to http://example.com/

  • Adding or removing trailing slash
  • http://example.com/display/ to http://example.com/display

    http://example.com/display to http://example.com/display/

  • Removing the directory index file name
  • http://example.com/display/index.html to http://example.com/display/

How it works

HttpModule

To handle every request to determine if there is a match to redirect, you have to subscribe to the BeginRequest event of the application.

Shown below is the code for RedirectRequest. It is important not to evaluate every URL pattern on every request because it can impact performance as this method is called on every request.

C#
public void RedirectRequest(HttpContextBase context, 
            RedirectorConfiguration config)
{
    HttpRequestBase request = context.Request;
    HttpResponseBase response = context.Response;

    string rawUrl = request.Url.AbsoluteUri;

    //Ignore FORM requests
    if (request.HttpMethod.ToUpper() != "GET" && 
        request.HttpMethod.ToUpper() != "HEAD")
    {
        return;
    }

    //Ignore Urls defined in config in order 
    //to avoid evaluation of this Urls (performance).
    if (!String.IsNullOrEmpty(config.IgnoreRegex))
    {
        if (Regex.IsMatch(rawUrl, config.IgnoreRegex))
        {
            return;
        }
    }

    //Check every Url group, if a group matches 
    //(only then) evaluate the individual Url patterns
    foreach (RedirectorUrlGroup group in config.UrlGroups)
    {
        //For each group of regular expression, check if the Url matches
        if (Regex.IsMatch(rawUrl, group.Regex))
        {
            foreach (RedirectorUrl Url in group.Urls)
            {
                //For each regular expression in the group check if it matches
                if (Regex.IsMatch(rawUrl, Url.Regex))
                {
                    string UrlResult = Regex.Replace(rawUrl, Url.Regex, Url.Replacement);
                    // 301: Moved permanently or 302: Found redirect
                    response.StatusCode = Url.ResponseStatus;
                    response.AddHeader("Location", UrlResult);
                    response.End();
                    break;
                }
            }
            break;
        }
    }
}

Let me summarize the above code.

  1. Ignore FORM requests.
  2. Ignore URLs that match the IgnoreRegex pattern.
  3. Group URL patterns to evaluate the minimum amount of if statements for every URL.
  4. Check every URL group; if a group matches (only then), evaluate the individual URL patterns.

Configuration

Use the configuration to determine the URL patterns and redirections.

Here is a sample:

XML
<redirector ignoreRegex=".*(\.css|\.txt|\.js|\.gif|\.jpg|\.png)">
    <UrlGroups>
        <add regex="/news-items/.+">
            <!-- Items redirection that are inside a folder-->
            <Urls>
                <add regex="/news-items/index.html" replacement="/news-items/" />
                <add regex="/news-items/article(\d+).html" 
                  replacement="/news-items/interesting-article$1.aspx" />
            </Urls>
        </add>
        <add regex="/press-releases.*">
            <!-- Folder redirection (change name). From /press-releases/ to /press/ -->
            <Urls>
                <add regex="/press-releases(.*)" replacement="/press$1" />
            </Urls>
        </add>
        <add regex="^http://www.contoso.com.*">
            <!-- Domain or subdomain redirection -->
            <Urls>
                <add regex="^http://www.contoso.com(.*)$" 
                  replacement="http://contoso.com$1" />
            </Urls>
        </add>
    </UrlGroups>
</redirector>

How to use it

To use it in your project, you need to:

  1. Register the HttpModule in the web.config file.
  2. XML
    <httpModules>
        <add name="RedirectorModule" 
          type="SampleRedirector.Modules.RedirectorModule, SampleRedirector"/>
    </httpModules>
  3. Add a configuration file, redirector.config, and reference it in web.config.
  4. XML
    <configSections>
        <section name="redirector" 
          type="SampleRedirector.Configuration.RedirectorConfiguration, SampleRedirector" />
    </configSections>
    <redirector configSource="Redirector.config"/> 
  5. Adapt redirector.config to your needs.

Hope you enjoy it!

History

  • January 19th, 2010 - Article submitted.

License

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


Written By
Software Developer
Spain Spain
Jorge has been working in Software development for more than 10 years. Born in Argentina, he lives in Spain since 2004.

He worked as a consultant for mayor companies including Log, HP and Avanade and holds some technical certifications including MCSD and MCAD.

He is the founder of the asp.net mvc forum open source project Nearforums, the Node.js Cassandra driver and the owner of the news release site prsync.com.

Follow him on Twitter: twitter.com/jorgebg

Contact: jorgebaygondra at gmail

Comments and Discussions

 
QuestionTesting Pin
tp_h24-Apr-14 2:56
tp_h24-Apr-14 2:56 
QuestionIs it Pin
Amit Developer12-Dec-12 22:47
Amit Developer12-Dec-12 22:47 
AnswerRe: Is it Pin
Jorge Bay Gondra12-Dec-12 22:59
Jorge Bay Gondra12-Dec-12 22:59 
GeneralRe: Is it Pin
Amit Developer13-Dec-12 0:21
Amit Developer13-Dec-12 0:21 
GeneralMy vote of 4 Pin
Kacha Jatin30-Jul-11 1:50
Kacha Jatin30-Jul-11 1:50 
GeneralFine Pin
thatraja23-Jan-10 17:18
professionalthatraja23-Jan-10 17:18 
Generalnice job Pin
Arlen Navasartian21-Jan-10 10:47
Arlen Navasartian21-Jan-10 10:47 
GeneralWell done Pin
Petr Pechovic20-Jan-10 1:05
professionalPetr Pechovic20-Jan-10 1:05 

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.