Saturday, November 04, 2006 4:56 AM bart

Blog on the move adventures - Simple URL rewriting

Yesterday I reported having moved my blog to another host, which went just fine. So, today I took a look at the stats that come with the hosting package to find out about the number of requests per day. I was pretty amazed to see about 14,000 hits per day. However, when diving a little deeper I saw that about 10% of the requests ended up as a error-notfound.aspx. Pretty strange.

So what was going on? In the past, before moving to Community Server, my blog was on http://blogs.bartrdesmet.net/bart. Later, when I moved to CS, I added a dummy website to IIS, redirecting all traffic for http://blogs.bartdesmet.net to http://community.bartdesmet.net/blogs which worked fine. Today however, everything is hosted in one flat IIS space, meaning that http://community.bartdesmet.net and http://blogs.bartdesmet.net point to the same physical file location. So, requests like http://blogs.bartdesmet.net/bart/archive/2005/10/28/3736.aspx result in a not found error.

The solution? I wrote a very simple URL rewriting tool that looks for requests starting with /bart and redirect those to the appropriate location, e.g. http://community.bartdesmet.net/blogs/bart/archive/2005/10/28/3736.aspx. Here's the code:

using System;
using System.Web;

public class UrlRewriter : IHttpModule
{
   public virtual void Init(HttpApplication app)
   {
      app.BeginRequest += new EventHandler(this.UrlRewriter_BeginRequest);
   }

   public void UrlRewriter_BeginRequest(object sender, EventArgs e)
   {
      HttpApplication app = (HttpApplication) sender;
      if (app.Request.Path.ToLower().StartsWith("/bart"))
         app.Response.Redirect("
http://community.bartdesmet.net/blogs" + app.Request.Path);
   }

   public void Dispose() {}
}

Tomorrow I'm leaving for TechEd. There will be a last technical post from home that appears online tomorrow afternoon. Stay tuned for TechEd adventure reports the next two weeks.

Del.icio.us | Digg It | Technorati | Blinklist | Furl | reddit | DotNetKicks

Filed under: ,

Comments

# Community Server Daily News for Tuesday, November 7, 2006

Tuesday, November 07, 2006 1:35 PM by Community Server Daily News

news of the day a grab bag for what's happening in Community Server Telligent launches BlogMailr a new,

# Bart de Smet writes a simple UrlRewriter to redirect requests

Wednesday, November 22, 2006 7:58 AM by Daily News Faq List

Bart de Smet writes a simple UrlRewriter to redirect requests of his recently relocated and url-modified

# Bart de Smet writes a simple UrlRewriter to redirect requests

Monday, March 12, 2007 4:19 AM by Community Server Bits

Bart de Smet writes a simple UrlRewriter to redirect requests of his recently relocated and url-modified