Sunday 26 July 2009

Simple URL Rewriting with ASP.NET and Managed Fusion UrlRewriter

You can download the example discussed in this post here.

Why rewrite?

The default URLs from ASP.NET Webforms (and possibly even MVC) are not fantastic for SEO (Search Engine Optimisation).

You know the sort:

http://example.com/Questions/Question.aspx?ID=345

Google doesn’t really like URLs like this. It doesn’t say much about the page it represents. It might have an idea that it’s something to do with question 345 but that could mean anything. The file extension (.aspx) doesn’t help either.

What Google likes to see, is something a bit more like this:

http://example.com/questions/345/how-do-i-do-something

Well with Webforms you’re a little stuck, as the URL is dependent on the file structure of your solution and name of the files.

Unless..

..you perform some voodoo URL rewriting.

Example setup

We are going to map an incoming request to a URL like this..:

http://localhost:52468/questions/1337/how-do-i-do-something

..to the default page URL like this:

http://localhost:52468/Default.aspx?ID=1337

To achieve this I’m going run through a very simple rewriting setup.

Nick has a more detailed example here and you should also check out Scott Guthrie’s post here. (N.B. My example is based on Nick’s)

First, create a standard web application:

createwebapplication

Then download UrlRewriter from Managed Fusion. Unzip it, put the dll and pdb in a directory of your choosing and add a reference to the dll.

addreference

Next, create a new text file called ManagedFusion.Rewriter.txt and place the following in it:

createrewritertxtfile

In your Web.Config, add (or copy from the example) these following sections:

configuration -> configSections ->

image

configuration ->

image

configuration -> system.web -> httpModules ->

image

configuration -> system.webServer ->

On the opening modules tag, set runAllManagedModulesForAllRequests to true and add the RewriterModule so is becomes:

image

configuration -> system.webServer -> handlers ->

image

And that’s it!

Note that the application actually ignores the slugs at the end of the URL so your app can generate what it likes for them.

Also note that the rewrite rules can be far more complex than what is shown in the example. UrlRewriter’s rules are based on the Apache module, mod_rewrite.

Further reading:

Monday 20 July 2009

Brighton ALT.NET Beers: Tuesday 4th August, 7pm at The Prince Albert


I know we talked about possibly of doing this one outside, but the weather has been too changable of late, so let's convene indoors.
At the pub.
Ace.
So I'm delighted to announce that the next Brighton ALT.NET beers will be:

Tuesday 4th August, 7pm at The Prince Albert.

If you're a .NET developer in the area and you haven't been yet, do come along. The group is super-welcoming. We want to hear your ideas about how you think software should be written.
Hope to see you there!
P.S. Hopefully we'll get a chance for the even-less-formal beers on the beach in Septemeber.