Get Started!

Rewrite or Redirect Old Site Links

If you're replacing an existing site one very important task is ensuring that any existing "Google Juice" is retained, by mapping the old site structure to the new site. A HTTP Module is the perfect tool for the task.

Rewrite or Redirect

It's up to you really, if you replacing an old site or just tweaking an existing site it's a choice of how important the old URL is. One would think most is a Perminant Redirection.

Either way the basic structure is the same.

Example

In the example below, we crete a Dictionary<string, string> to act as an in-memory cache of the URLs. When a request is recivied the keys index of the Dictionary is looked-up, if no match is found the method returns, else we grab the destination path from the dictionary and etither Rewrite or Redirect.

using Net4orce.__HTTP_Handlers__;

namespace Example
{
  public static partial class ExampleApp 
  {
    static ExampleApp(){
      registerModules();
    }

    // Cache of values for lookups
    private static Dictionary<string, string> dict = new Dictionary<string, string>{
      {"/path_1", "/new/path-one.html"},
      {"/path_2", "/new/path-two.html"},
      {"/path_3", "/new/path-three.html"},
    }

    private static void registerModules(){

      DynAssDelegateModule.RegisterAction(

        // Key / Ident for Module
        @"map-old-links",

        // The main thread for the module
        (httpApp) => {

          // The requested path
          var pathIn = httpApp.Context.Request.Url.AbsolutePath;

          // Look up the index of keys
          if( !dict.ContainsKey(pathIn) ) {

            // no match return asap
            return;
          }

          // Matched - Get the destination Path
          var pathOut = dict[pathIn];

          // Either
          httpApp.Context.Response.Rewrite(pathOut);

          // Or
          httpApp.Context.Response.Redirect(pathOut);

        }
      );

    }
  }
}

And that's it!

Well maybe

I find it best to use more verbose responses when mapping sites to sites.

More complete Redirect

The example above works I prefer to use :

httpApp.Context.Response.Redirect(action.RedirectPath, false);
httpApp.Context.Response.StatusCode = 301;

httpApp.Context.Response.Flush();
httpApp.Context.Response.End ();

Gone!

Little used, but reduces load when transfering sites. 410 AKA Gone is like 404 AKA Not Found but more aggresive. Search engine treat 410 like 404's they will still come back & check for content, just less frequently.

As there's a limit to the number of requests an engine will make to a given site during the day, so for each of the reduced visits there's an extra visits avaliable to the more relevant content within the site.

In turn rasing the importance of the other, relevant, pages.

Do use it with care however!

httpApp.Context.Response.StatusCode = 410;

httpApp.Context.Response.Flush();
httpApp.Context.Response.End ();