Shane asks:
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: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.
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.
Archived 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/els... 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.
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/ )
You can create rules using URL Rewrite Tool found here http://webtools.live2suppor...
All, thanks for sharing. URL re-write is pretty new to me.
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...
but the URL needs to look
http://somepage.siteTwo.com
Thank you. Any help would be greatly appreciative
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?
thanks Tom
If I set up a subdomain would it still show the CF URL or the subdomain URL?
@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.
@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.
Guys,
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!
.htaccess is not PHP specific - it is web server specific (Apache), so yes, you can use it with CF.
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"/>
</conditions>
<action type="Redirect" url="blog/{R:1}" appendquerystring="true" redirecttype="Permanent"/>
</rule>
Sorry no.
Thanks. For some odd reason IIS rewrites are not working... but I may have made a mistake or three!