all posts

WhatIWantMost Url Rewriting

Published to Blog on 11 Jan 2007

I made a goof in my last post. I originally wrote that I was using UrlRewriter.net, when in actuality I am using UrlRewritingNet.UrlRewrite. Because of the similarities in the names and urls and last night’s interest in some other extracurricular activities* I’m sure you can understand and forgive. I have no doubt that UrlRewriter.net is a good tool, too, I just chose to use UrlRewritingNet.UrlRewrite because I have used it before and I like it. No harm, no foul.

(* Sam Adams Holiday Porter is a fantastic beer and I’m especially fond of their Winter Lager. Sam Adams Octoberfest was good, too, but I liked Goose Island’s Octoberfest beer better.)

Anyway, a little birdie asked for more details on how I was using Url rewriting. So I thought I’d share them.

To setup UrlRewritingNet.UrlRewrite, let’s call it UrlRewrite from here on out, you just drop the dll in your bin folder, add the following to your “configSections” of your web.config:

<section name="urlrewritingnet"
    requirePermission ="false"
    type="UrlRewritingNet.Configuration.UrlRewriteSection, UrlRewritingNet.UrlRewriter"  />

Add UrlRewrite to the httpModules section:

<add name="UrlRewriteModule" type="UrlRewritingNet.Web.UrlRewriteModule, UrlRewritingNet.UrlRewriter" />

Add a Urlrewritingnet section to your web.config similar to this:

<urlrewritingnet
   rewriteOnlyVirtualUrls="true"
   contextItemsPrefix="QueryString"
   defaultPage = "default.aspx"
   defaultProvider="RegEx"
   xmlns="http://www.urlrewriting.net/schemas/config/2006/07" >
        <rewrites>
            <!\-\- your rules go here -->
        </rewrites>
</urlrewritingnet>

And then define your rules. I won’t go into detail covering the various rules and settings as that is covered more than well enough in the documentation with plenty of examples. The download even comes with a sample site. Currently I only have one rule in place:

<add name="Wishlist"
    virtualUrl="^~/(.*)/Wishlist.aspx"
    rewriteUrlParameter="ExcludeFromClientQueryString"
    destinationUrl="~/Wishlist.aspx?id=$1"
    ignoreCase="true" />

For WhatIWantMost I have to pass in a parameter, the username, to the Wishlist page telling it what user’s wishlist I would like to see. If there is no username passed in then the page checks to see if I am logged in. If so it shows me my wishlist. If neither of the above is the case then I get redirected to the login page.

When passing in a username, the url looks like this: http://www.whatiwantmost.com/Wishlist.aspx?id=bobby. The page understands what that means, but it is a little ugly. It would be much better if it looked like this: http://www.whatiwantmost.com/bobby/Wishlist.aspx. That is exactly what my UrlRewriter rule defined above allows. I can provide a link that looks like the second Url (the pretty one) and the page understands it as the ugly one. In this example I am only using one parameter, the username, but I could use more if I needed.

 

That’s all there is to it, really. I hope that clarifies the point I too quickly covered in the previous post. As time goes on I will add more rules for UrlRewriter; the User Profile page will be another good opportunity to do so.


Dan Hounshell
Web geek, nerd, amateur maker. Likes: apis, node, motorcycles, sports, chickens, watches, food, Nashville, Savannah, Cincinnati and family.
Dan Hounshell on Twitter


  • On 12 Jan 2007 "Dave Burke"" said:
    Wow, you even do requests! What a great guy! Very interesting. I've seen guys do this along with CS's rewriting. Seems unnecessary, but if it works...
  • On 12 Jan 2007 """ said:
    Dave, I realized after you asked that I had skimped on the details. I can't imagine CS's rewriting not being able to cover anything you can come up with, but to each his own.
  • On 6 Jul 2007 "Digging My Blog - Dan Hounshell"" said:
    Following is a list of all the posts of the WhatIWantMost series. I'm listing them all here mostly for