November 24, 2009 | In: .Net

URL Rewriting

So, I started this blog with high hopes and dreams and only a few weeks in and I can’t think of a decent thing to write about.  So, like I will probably do time and again on this blog I checked out StackOverflow, found an answer I posted that others seemed to like and voila – instant blog post.

What is URL Rewriting?

Url rewriting is the process of taking something that looks like this:

http://www.shoes.com/Shopping/ProductDetails.aspx?p=EC1177024&pg=5105736

And turning it into something a human can understand.  Something that might look like this:

http://www.zappos.com/dr-martens-tevin-dark-brown-overdrive

Both go to the same shoes by the way.

Why URL Rewriting?

There a many reasons to user rewriting, let’s break down some of the most important:

  • Google – All else being equal, if I were to Google up “Dr. Marten’s Tevin Shoes” which of the above URL’s would Google prefer?  Zappos.com is a top results while shoes.com falls somewhere around page 4.  And then there’s the little issue of page rank as Jeff Atwood talks about here.

 

 

How?

Ah yes, the critical how question, well the answer very much depends on the technology stack.  I can’t cover every stack here of course so I’ll cover at least a little to give you an idea of how it’s done.

In .Net

In .Net these days there are 2 flavors of applications – MVC and web forms.  MVC apps come with their own built in URL rewriting engine.  It’s covered pretty extensively here.

In the world of web forms there are a lot more shades of gray.  You can always get a product like the one from Managed Fusion and let it do all the heavy lifting for you.  But where’s the fun it that?

But more seriously, there are times you don’t want dependency on a 3rd party library.  Maybe you’re creating your own product to sell.  Maybe you’re creating an open source tool and don’t want the dependency.

Well for web forms URL rewriting you have a couple of options.  Scott Guthrie covers them all pretty well here.  If you’re in a single application, I find the best approach to be creating your own HTTPModule.  You can download Scott’s sample app and check out how it’s done.

Non-Windows Servers

The gold standard in url rewriting is  the Apache mod_rewrite module.  It’s actually the basis of most IIS rewrite engines.  So if you’re using php, jsp, ruby, etc with Apache – this is where you want to look to do your url rewriting.

So to recap

  • Rewrite your URL’s
  • If you’re using .Net, first consider MVC, second consider creating your own HTTP Module
  • For everything else, try to use the Apache mod_rewrite module.

Comment Form