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

Anthony Webb's Gravatar 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
Tom K's Gravatar 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
shimju david's Gravatar 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
Sameer Gupta's Gravatar All, thanks for sharing. URL re-write is pretty new to me.
# Posted By Sameer Gupta | 6/23/08 5:17 AM
TN's Gravatar Hi

Is it possible to rewrite URL to a different server?

For example, I need the following:

http://somepage.siteTwo.com (does not run CF)

calls

http://www.siteOne.com/test.cfm

but the URL needs to look

http://somepage.siteTwo.com

Thank you. Any help would be greatly appreciative
# Posted By TN | 12/15/08 5:56 PM
Tom k's Gravatar Simply put, no. In that example you would be asking another web server to run code on a completely different machine. You could set up a sub domain and point it at the cf server though?
# Posted By Tom k | 12/15/08 6:04 PM
TN's Gravatar thanks Tom

If I set up a subdomain would it still show the CF URL or the subdomain URL?
# Posted By TN | 12/15/08 6:07 PM
Raymond Camden's Gravatar @TN - Afaik, yes, you can do this. Obviously if you read the docs, you will see. Worst comes to worst, you can do it server side with cflocation.
# Posted By Raymond Camden | 12/15/08 8:01 PM
Raymond Camden's Gravatar @TN - I'm sorry. I did not closely read your comment. TomK is absolutely right, and again, forgive me. I took the day off and my brain is still on vacation.
# Posted By Raymond Camden | 12/15/08 8:02 PM