Keep Your Link-Juice by Using a Custom HTTP Module for 301 Redirects

In my process of turning www.aspforblondes.com into www.crazeegeekchick.com I had some concerns regarding broken links, search engine rank etc.  When I registered crazeegeekchick, I just pointed it at the same IP as aspforblondes via DNS.   I thought at first that I'd just create a Chalk Extension to swap the titles of the domains based on the url, but quickly realized that this was a less than ideal approach.  The problem was that there would still be two instances of the same content on the web, and Google would penalize me  (See Google's policy on dupliate content).  I also realized that I'd lose my page ranking.  Killing aspforblondes.com was not an option.  The problem of Google viewing www.crazeegeekchick.com and crazeegeekchick.com as different sites was already handled for me in the configuration for Graffiti CMS, (you can also set this up in the Google Webmaster tools)  so I only had the problem of the two different domans.

I needed a solution that would allow me to reach the following goals:

  • No dupliate content penalty
  • No broken links from aspforblondes.com
  • Page ranking in tact
  • One stop for which to monitor analytics

I decided to write an ASP.NET custom HTTP Module to achieve my goals.  After reading tons of information about 301 permanent redirects and finally Chris Love's post on ASP.NET HTTP Modules,  The concept became more clear to me.  I preferred to handle this myself instead of involving my web host (Although Orcsweb was TOTALLY willing to help me out).

HttpModules are simply classes that can reside either in your Web site's code or in a separate class library. They are plugged into the life cycle of each request run through the ASP.NET engine. Typically they hook into events in the request life cycle and can react to actual request or modify the output in the Response object before it is sent to the client.

 Here are the steps I took to create my module:

  • Created a new class library project in Visual Studio
  • Added a specific reference (via a right click on the References folder) to the System.Web dll
  • Created a class that derives from IHttpModule
  • Created a method that returns a 301 Response Status Code if the requested path contains "aspforblondes.com"
  • Added a header within this method for Location that replaces "aspforblondes.com" with "crazeegeekchick.com"
  • Compiled into a dll
  • Uploaded the dll into my website's bin folder
  • Added a web.config entry for the custom HTTP Module

 

Custom HTTP Module Code (C#)

   1:  using System;
   2:  using System.Web;
   3:   
   4:  namespace _301RedirectModule
   5:  {
   6:      public class RedirectModule : IHttpModule
   7:   
   8:      {
   9:          public void Dispose()
  10:          {
  11:   
  12:          }
  13:   
  14:          public void Init(HttpApplication context)
  15:          {
  16:              // In the Init function, register for HttpApplication 
  17:              // events by adding your handlers.
  18:              context.AuthorizeRequest += (new EventHandler(Process301));
  19:          }
  20:   
  21:          public void Process301(object sender, EventArgs e)
  22:          {
  23:              HttpApplication app = (HttpApplication)sender;
  24:              HttpRequest request = app.Context.Request;
  25:              string lRequestedPath = request.Url.ToString();
  26:   
  27:              if (lRequestedPath.Contains("aspforblondes.com"))
  28:              {
  29:                  // set status to 301 - Perm Redirect
  30:                  app.Response.StatusCode = 301;
  31:                  
  32:                  // add location header for crazeegeekchick.com
  33:                  app.Response.AddHeader("Location", lRequestedPath.Replace("aspforblondes.com",
                       (continued from previous line)   "crazeegeekchick.com"));
  34:                  
  35:                  app.Response.End();
  36:              }
  37:          }
  38:      }
  39:  }

 

Custom HTTP Module Code (VB.NET)

   1:  Imports Microsoft.VisualBasic
   2:  Imports System.Web
   3:   
   4:  Public Class CrazeeGeekChick301
   5:      Implements IHttpModule
   6:   
   7:   
   8:      Public Sub Dispose() Implements System.Web.IHttpModule.Dispose
   9:   
  10:      End Sub
  11:   
  12:      Public Sub Init(ByVal context As System.Web.HttpApplication) Implements 
               (continued from previous line)System.Web.IHttpModule.Init
  13:          AddHandler context.AuthorizeRequest, AddressOf Process301
  14:      End Sub
  15:   
  16:      Public Sub Process301(ByVal sender As Object, ByVal e As EventArgs)
  17:          Dim app As HttpApplication = CType(sender, HttpApplication)
  18:          Dim Request As HttpRequest = app.Context.Request
  19:          Dim lRequestedPath As String = Request.Url.ToString
  20:   
  21:          If lRequestedPath.Contains("aspforblondes.com") Then
  22:              app.Response.StatusCode = 301 ' make a permanent redirect
  23:              app.Response.AddHeader("Location", lRequestedPath.Replace("aspforblondes.com", 
                      (continued from previous line)"crazeegeekchick.com"))
  24:              app.Response.End()
  25:          End If
  26:      End Sub
  27:  End Class

 

In My web.config, I simply had to place an entry within the <system.web> element to add my module:

<httpModules>
      <add name="CrazeeGeekChick301" type="CrazeeGeekChick.CrazeeGeekChick301, CrazeeGeekChick301" />
</httpModules>

(name = ModuleName   type = className, assemblyName)

 

Now, when a request is sent for aspforblondes.com, it goes directly to crazeegeekchick.com.  The 301 header tells Google not to index content beneath aspforblondes.com, and I still get to keep my link juice!  Pretty neat huh?

 

kick it on DotNetKicks.com

12 Comments and Trackback(s)

Leave a Reply

 
© Crazeegeekchick.com | Theme design by Dana Coffey | Powered by GraffitiCMS