advanced web statistics

Rewrite URL Without Using Filenames or Extensions (C#)

4/1/2006 12:23:33 PM

I`ve finally done it! People have been asking on my previous post about URL Rewriting via Global.asax if it is possible to rewrite a URL without using .aspx, .asp, .html, or any other file extension.  It seemed that the general consensus was that settings needed to be changed in IIS or that it simply wasn`t possible.  Well my friends, I have figured it out.  Building upon my theory of creating post slugs for each entry.

Before we begin let me explain what a "slug" is.  There have been many definitions I have come across trying to define them but the best that I've found is that they are a unique page identifier used (mainly by blog and commerce sites) for the purpose of making a more human and search-engine friendly URL.

Example
http://www.willasrari.com/blog/post.aspx?post=404&date=102005&t=Welcome (bad)
http://www.willasrari.com/blog/title-of-my-post-or-product-title-goes-here/ (good)

You can see that the bottom URL looks friendly and it doesn't take a rocket scientist to figure out that if you submit sites with many URL's following this convention to search engines that you will get better rankings. You must also name your entries (or slugs) very descriptively (word?) and relevant to the content of the entry and yada, yada, yada.... But you get the idea right?

How to accomplish this?
First you must use a field in your database for a slug. If you don't already have one (which I'm sure many of you don't) you can easily create one.  Just create a field for text values and loop through your entry titles creating the slugs.  They have to be unique!  I will post code for this at a later time.

Once you have a unique identifier (slug) for each entry you can then create a Regular Expression in your Global.asax file in the Application BeginRequest method to look for everything between "/" and "/" in the current URL. So /directory/blog-post/ would be pulled. Then you run this through a replace function to get "blog-post" and send this string to your database to get the ID for the entry (or product) and then rewrite the path to appropriate page.

Global.asax Code (C#) (Application_BeginRequest)

protected void Application_BeginRequest(object sender, EventArgs e)
{
   HttpContext httpContext = HttpContext.Current;
   Regex pattern = new Regex(@"(.+)\/", RegexOptions.IgnoreCase);
   Match matches = pattern.Match(httpContext.Request.Path);
   string slug = matches.ToString().Replace("/Rewrite/", "").Replace("/", "");

   if (!string.IsNullOrEmpty(slug))
   {
      int pageId;

      using (OleDbConnection cn = new OleDbConnection(""))
      using (OleDbCommand cm = new OleDbCommand("GetEntryIdBySlug", cn))
      {
         cn.Open();
         cm.CommandType = CommandType.StoredProcedure;
         cm.Parameters.AddWithValue("@slug", slug);
         pageId = (int) cm.ExecuteScalar();
      }

      httpContext.RewritePath(string.Format("~/entry.aspx?ID={0}", pageId));
   }
}

I have included the Visual Studio 2005 Solution file. I also do not have time (at this time) to fully explain every line of code. The solution file that I provide is well-commented.  I do plan on creating another entry that steps through the concept more.

One Last Thing
You might be wondering how to create a slug field that is browser/url-friendly. Here is the method that I use.

public string CreateFileSlug(string original)
{
   return Regex.Replace(original, @"[^\w\@-]", "-").ToLower();
}

Thanks to Bobby for engaging in dialogue and inspiring me to look more into this. The solution file is a directory named Rewrite. Simply extract the contents of the .zip file into a directory in wwwroot called Rewrite.

Have fun! Please link to this article if you find my code useful. I'm sure there are plenty of people that would benefit from this.

UPDATE
I am in the process of rewriting this to extend functionality. Possibly with & without slugs!

Attachments
Download the Visual Studio 2005 Solution, Microsoft Access 2003 Database

.NET, C#, Code, Programming, SEO

kick it on DotNetKicks.com

Leave a Comment

   

  Enter the text to proceed!