Ask a Jedi: URL Rewriting example

This post is more than 2 years old.

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 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 * url syntax. (You can download the code base for RIAForge here.) Another example of this is CFLib. It uses the form: You can download the code base for that as well (from this entry).

I hope this answers your question.

Raymond Camden's Picture

About Raymond Camden

Raymond is a developer advocate for HERE Technologies. He focuses on JavaScript, serverless and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA