ASP.NET URL Rewriting Made Easy

Jul

08

posted at: 5:47 PM

URL Rewriting can be a bit of a scary subject, particularly if you've never implemented it before. Apache has a humorous comment regarding their own implementation of URL Rewriting on their mod_rewrite page. "But all this functionality and flexibility has its drawback: complexity. So don't expect to understand this entire module in just one day." mod_rewrite can have it's share of complexity, as it's essentially regular expression based. Regular Expressions can get tricky in a hurry due to their complexity to write and difficulty to read, and if you're like me, you avoid them unless it really is the best way to accomplish an objective.

ASP.NET has it's own flexibility for URL rewriting as well. Some examples suggest you handle rewriting in the global.asax. This is pretty easy, but is generally not the best way to handle rewriting, as the global.asax is application specific, which makes it difficult for you to re-use your code later in a different project. The more acceptable method is to write your own HttpModule to handle the rewriting, and then reference that in the web.config. This is not too bad either if you've played around with HttpModules before, and can give you a ton of portable programmatic flexibility.

It's all greek to me

You're probably scratching your head at this point wondering how this is going to be easy. So far, getting this done in a .NET app is going to require a lot of work. Well, the good news is that there is an easy way, and we're going to walk through the steps of how you can get your own rewriting up and running in less time than it takes to make a peanut butter and jelly sandwich. Okay, thats a blatant exaggeration, but you get the idea.

The easiest way to get up and going if you're developing with the .NET platform is to leverage code that someone else has written. There is a great component over at urlrewriter.net that is very easy to use, and is what I'll be using for this example. First, head over to urlrewriter.net and download the module. The next thing you'll want to do is unzip the file to a handy location, and open Visual Studio. You're going to want to add a reference to their binary DLL, so right click on your web application project, and choose Add Reference.

Add Reference

We'll need to browse for the actual DLL, so choose the browse tab, and then navigate to where you unzipped the download. You are going to want to reference the Release DLL, so click on the bin folder, and then Release, and select the Inteligencia.UrlRewriter.dll.

Updating the web.config

The first area you will need to add is a new config section to the web.config, and reference the namespace of the DLL. Give your section a name of "rewriter", and then a type of "Intelligencia.UrlRewriter.Configuration.RewriterConfigurationSectionHandler, Intelligencia.UrlRewriter".

Add Section

Once you've added a config section, you will need to add a node to the HttpModules node in system.web. If the HttpModules node doesn't already exist, go ahead and create it. The nested node should have a name of "UrlRewriter" and a type of "Intelligencia.UrlRewriter.RewriterHttpModule, Intelligencia.UrlRewriter".

Add HttpModule

Lastly, at the bottom of your web.config before the end of the configuration node, add a node called "rewriter". This will be where you will add your rewriting rules. Don't worry, these are actually really easy to do. The nodes nested in here will be "if" nodes, with attributes of "url" and "rewrite". The url attribute represents what the rewriter will be looking for in the actual URL entered by the user. Rewrite will be what file the application will map it to.

Okay, great. Now what?

Well, this rewriter component has a lot of flexibility with regular expressions, as any implementation ought to. Whats advantageous to using this method, is that it is very easy to quickly create one to one mappings, as well as adding more flexibility to dynamic pages. For example, you could map http://www.elan3.com/wire/wire.aspx directly to something like: http://www.elan3.com/page.aspx?id=120.

You're probably thinking that might be nice occaisonally when I need static mappings, but what about something more dynamic and adaptable? Well, this rewriter contains some special syntax for handling that. As the image illustrates below, a url of "/posts/(.+)" is really looking for any url that contains text in the posts directory. The rewriter takes whatever is after posts, and encapsulates it into a variable for you to use in the rewrite attribute, in the form of: $1.

This way, I can map it directly to: /blog/BlogEntry.aspx?datetitle=$1. So a request with a URL of http://elan3.com/posts/2007/07/08/The-Web-is-Tough.aspx gets mapped to: http://elan3.com/blog/BlogEntry.aspx?datetite=2007/07/08/The-Web-is-Tough.aspx. Unless you are hosting on a server running IIS 7, you'll need to have .aspx at the end of all of your URLs. You could get fancy with an ISAPI module to change the extension to something cooler, but thats getting a little bit off-topic.

Rewriter Rules

Putting it all together in code

From here on out, it really becomes more about writing your own business logic to handle what relevant data being passed in the friendly URL. For the purpose of this example, the image below shows how to simply parse the contents of the URL we've been using to turn it into meaningful data you can query with. Naturally, your own implementation will depend on the structure of your web application, as well as the requirements for your rewriting.

C# Code post a comment {9}

Comments

  • 1

    elan says... Jul 10 | 11:08 PM

    And to think in the time it took me to write the post, I could have made nice code-block css. Scottgu uses images, so it can't be that bad, can it?

  • 2

    dzxfctvan@gmail.com says... Aug 10 | 04:23 PM

    dmkiuyqsp cwmfsabqu dfkum epgxfkbh nzhmxrvwb kpusweajz ytsacl

  • 3

    kral oyun says... Nov 04 | 10:30 AM

    thanks

  • 4

    ivan says... Jan 20 | 06:16 AM

    how to make it work in IIS 7? after migrating from IIS 6

  • 5

    Gareth says... Feb 20 | 08:57 AM

    Hi, i am interested in using url rewritting but have run into an issue. my site is layed out in a folder structure, if i have all my files in the root of the site then my themes work fine but as soon as i put them into folders they break. Do you know of a rewrite rule i can use to get them working in folders?

  • 6

    Maxx14 says... Oct 10 | 04:26 PM

    Nothing of the sort happens with our permanganate molecules. ,

  • 7

    JXL64 says... Oct 13 | 02:06 AM

    But while much too little and too late, this deval- uation was significant in violating an endless round of offi- cial American pronouncements, which had pledged to maintain the $35 rate forevermore. ,

  • 8

    Daddy22 says... Oct 22 | 02:21 AM

    They dug her grave and nothing will change that. ,

  • 9

    Coder67 says... Oct 23 | 01:56 AM

    Perhaps society needs to go further and actively promote birth control and more active rewards for moving in the direction of middle class values of delayed parenthood, two parent families, and a more traditional lifestyle. ,

Post Commentary

Name: *

 

Website / Blog:

Comment: *