Ask a Jedi: URL Rewriting example

Shane asks:

Appreciate all your posts on the isapi URL rewrite component however I am unable to find any information online pertaining to what I want to do with it.

I'd like to accomplish the http://www.site.com/user URL which myspace and flickr have. where /user would be user.cfm?user=shane or whatever.

I'd rather avoid using the onMissingTemplate hack and use the rewriter for this, I've been fiddling for a while.

Before I begin - note that what I say here pertains to both IIS rewriting as well as Apache rewriting. So to begin with, the rule in question is rather simple. All you want to do is replace /X with /user.cfm?user=X. I used this rule in Apache:

RewriteRule ^/([A-Za-z0-9]+)$ /user.cfm?user=$1 [PT]

This may not be the best rule but it seemed to work well for me. Basically it matches /anynumberletter(end of url). It then forwards the request to user.cfm and appends the value of anynumberofletter to the url scope.

I believe in taking baby steps, so this is what I wrote for user.cfm:

<cfdump var="#url#">

I did some quick tests and ensure the URL scope was being set right.

Now at this point what you do is really dependent on your application. If we assume usernames are unique (I certainly hope so), you want to look up the user record based on the username. Once you have that, you can do whatever you want of course. Greet the user by name, show their favorite background color, or whatever. While not using URL Rewriting, RIAForge does this with it's *.riaforge.org url syntax. (You can download the code base for RIAForge here.) Another example of this is CFLib. It uses the form: http://www.cflib.org/udf/isemail. You can download the code base for that as well (from this entry).

I hope this answers your question.

Comments

I am sure there are many ways to skin this cat, and our requirements were a little different, Our rewrite rule is a little different and I thought I'd share.

The problem we hit was coming up with a rule that would work for http://url.com/somthing/else/?this=that and pass everything to our controller for some creative parsing. Our original rule was also rewriting all our JS file urls which was bad. In the end we came up with this, and have been using it tried and true:

RewriteCond %{REQUEST_FILENAME} ^.*?[^.]{5,}$
RewriteRule (.*) /index.cfm?$1 [L]

So long as the url doesnt look like a file on the server it will pass everything on to the controller where we parse and act appropriately on the url.
# Posted By Anthony Webb | 6/21/08 6:14 PM
I find these rules quite useful - matching words vs integers

RewriteRule /events/ /events\.cfm\ [I]
(Would rewrite /events.cfm as /events/)

RewriteRule /events/(\d+)/ /events\.cfm\?id=$1 [I]
(Would rewrite /events.cfm?id=1 as /events/1/ )

RewriteRule /events/(\w+)/ /events\.cfm\?var=$1 [I]
(Would rewrite /events.cfm?var=foo as /events/foo/)

RewriteRule /events/(\w+)/(\w+)/ /events\.cfm\?var1=$1\&?var2=$2 [I,L]
(Would rewrite /events.cfm?var1=foo&var2=bar as /events/foo/bar/ )
# Posted By Tom K | 6/22/08 4:01 AM
You can create rules using URL Rewrite Tool found here http://webtools.live2support.com/misc_rewrite.php
# Posted By shimju david | 6/22/08 12:58 PM
All, thanks for sharing. URL re-write is pretty new to me.
# Posted By Sameer Gupta | 6/23/08 5:17 AM