The title of this post/article probably doesn’t make sense to most people. In my head I was trying to come up with a title and nearly every word was UrlRewriting; it got me thinking of the scene in Supertroopers where they see how many times they can say meow while they pull someone over.
Ok, enough of that train of thought – what we want to get to is how to do UrlRewriting, which is of course, how to convert url’s such as http://www.nicholasbarger.com/default.aspx?blogid=12092 to http://www.nicholasbarger.com/11-UrlRewriting_with_UrlRewritingNetUrlRewriting_(Supertroopers_-_”meow”). But wait! That URL is much longer and still ambiguous, why would I want to do this?
There are really two main advantages. First, it’s slightly easier to bookmark and recall because it is more text based rather than numeric, but the primary reason is because of search engine visibility. Search engine results are a bit better when the actual name of the webpage relates to the content found on the page; similar to the title of the page matching keywords for the page content.
What We Need
To get started, there are several ways to use UrlRewriting; you can write your own rewriting engine, you can use MVC (includes a routing engine, see article from Scott Guthrie), or you can use one of the prewritten UrlRewriting components that are already out there and have been time-tested. I picked up one several years ago which I use for many of my projects that require UrlRewriting which will be what I use to demo for this posting. You can find it at the following address (http://www.urlrewriting.net/)
I’m sure there are detailed directions in the installation packet that you download, but here is a quick step-by-step for barebones installation:
- Reference the UrlRewritingNet.UrlRewrite.dll in your project.
Add the following in your webconfig file in the “<configSections></configSections>”:
<section name="urlrewritingnet" requirePermission="false" type="UrlRewritingNet.Configuration.UrlRewriteSection, UrlRewritingNet.UrlRewriter" />
Add the following in your webconfig file in the “<configuration></configuration>”:
Note: A sample rule is included which I will explain how to modify and create your own rules for UrlRewriting.
<urlrewritingnet rewriteOnlyVirtualUrls="true" contextItemsPrefix="QueryString" defaultPage = "default.aspx" defaultProvider="RegEx" xmlns="http://www.urlrewriting.net/schemas/config/2006/07" > <rewrites> <add name="blogpost" virtualUrl="^~/([0-9]+)-(.*)" rewriteUrlParameter="ExcludeFromClientQueryString" destinationUrl="~/default.aspx?blogid=$1" ignoreCase="true" /> </rewrites> </urlrewritingnet>
Add the following in your webconfig file in the “<httpModules></httpModules>”:
<add name="UrlRewriteModule" type="UrlRewritingNet.Web.UrlRewriteModule, UrlRewritingNet.UrlRewriter" />
Now We’re Set, How Do You Use It?
The UrlRewriting engine looks for a URL that matches regular expression based rules you define in the webconfig. It then matches the parameters and reforms them into the destination URL in the specified format.
All you have to do is use links throughout your pages that match the virtual URL rule you specified.
<a href="http://www.nicholasbarger.com/11-UrlRewriting_with_UrlRewritingNetUrlRewriting_(Supertroopers_-_meow)">View Article</a>
This URL doesn’t actually exist, but the UrlRewriting engine reads it and redirects to the appropriate URL (http://www.nicholasbarger.com/default.aspx?blogid=11)
Creating Your Own Rules
To really get good at this, you need to be a little familiar with regular expressions. I’ll be honest, I never remember off the top of my head regular expressions so I tend to Google a primer on it whenever I sit down and work with these (or lookup old examples for basic pattern matching).
You may want to do the same, click here to start searching!
Remember, the virtualUrl is the address that you will type into your links, it’s also what the user will see in the address bar. The destinationUrl is where the real page lies. You can use $ and then the corresponding number (in order of regular expression combinations) to retrieve the value the regular expression is matching. In my case, I’m getting the blogid which i’ve placed as the prefix to the post/article title.
Good luck with UrlRewriting and play around a bit to discover some of the benefits of using UrlRewriting.
Note: Be sure to be careful when using /, slashing characters in your url’s, this does denote you are in a directory higher/lower than you actually may be; so images or other references may break if using relative paths.