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 senior developer evangelist for Adobe. He focuses on document services, JavaScript, 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

Archived Comments

Comment 1 by Anthony Webb posted on 6/22/2008 at 3:14 AM

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 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.

Comment 2 by Tom K posted on 6/22/2008 at 1:01 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/ )

Comment 3 by shimju david posted on 6/22/2008 at 9:58 PM

You can create rules using URL Rewrite Tool found here http://webtools.live2suppor...

Comment 4 by Sameer Gupta posted on 6/23/2008 at 2:17 PM

All, thanks for sharing. URL re-write is pretty new to me.

Comment 5 by TN posted on 12/16/2008 at 4:56 AM


Is it possible to rewrite URL to a different server?

For example, I need the following: (does not run CF)


but the URL needs to look

Thank you. Any help would be greatly appreciative

Comment 6 by Tom k posted on 12/16/2008 at 5:04 AM

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?

Comment 7 by TN posted on 12/16/2008 at 5:07 AM

thanks Tom

If I set up a subdomain would it still show the CF URL or the subdomain URL?

Comment 8 by Raymond Camden posted on 12/16/2008 at 7:01 AM

@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.

Comment 9 by Raymond Camden posted on 12/16/2008 at 7:02 AM

@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.

Comment 10 by Ajay posted on 2/16/2010 at 5:27 PM


I am little bit confused! For adding rules which file need to add?
.htaccess like we do in PHP or something else!
Please guide i am new in coldFusion!

Comment 11 by Raymond Camden posted on 2/16/2010 at 6:44 PM

.htaccess is not PHP specific - it is web server specific (Apache), so yes, you can use it with CF.

Comment 12 by Gregory posted on 10/5/2019 at 7:51 PM

Hi Raymond,
Did you ever make a IIS rewrite rule for BlogCfc? I am having difficulties getting anything to work with my new Galaxie Blog fork. I used the following code, but it gives me a 500 error every time:
<rule name="GalaxieBlog" stopprocessing="true">
<match url="blog/(.*)index.cfm"/>
<conditions logicalgrouping="MatchAll">
<add input="{REQUEST_METHOD}" pattern="^POST$" negate="true"/>
<add input="{SCRIPT_FILENAME}" matchtype="IsFile" negate="true"/>
<add input="{QUERY_STRING}" pattern=".+" ignorecase="false" negate="true"/>
<action type="Redirect" url="blog/{R:1}" appendquerystring="true" redirecttype="Permanent"/>

Comment 13 (In reply to #12) by Raymond Camden posted on 10/6/2019 at 5:11 AM

Sorry no.

Comment 14 (In reply to #13) by Gregory posted on 10/6/2019 at 5:24 AM

Thanks. For some odd reason IIS rewrites are not working... but I may have made a mistake or three!