Tech Blog

Easy cross platform and environment file paths

Posted At : March 12, 2007 10:26 PM

One of the very useful and underrated functions in ColdFusion is the humble ExpandPath function. It has saved my life on many occasion and I just thought I'd explain how it works and what it does.

If you or dealing with file or directory manipulation on from CF then you will often need a way to get a file system path. You could just use it as follows:

<cfdirectory action="list" name="dirList" directory="c:\farcry\myApp">

This is of course very nasty as when you put this onto your live server you may not have farcry installed in c:\ - it may be somewhere completely different (particularly on shared hosting). Alternatively your live server may be a linux box in which case there is no "C:" drive at all.

To solve this and make you applications nice an portable you can use expandPath on a cf mapping. So assuming you have a cf mapping for /farcry to c:/farcry you would use the following:

<cfdirectory action="list" name="dirList" directory="#expandPath('/farcry')#/myApp">

This will now happily run on any machine that has the farcry mapping and will map to whatever location the mapping points to.

Cheers, Mark

Flexible Friendly URLs for Farcry with Mod rewrite (Updated)

Posted At : January 22, 2007 1:00 PM 3 Comments

I've discovered a problem in the regex that I put in the previous blog I wrote about using mod rewrite for friendly urls.

It's nothing terminal and can very easily be fixed and would probably have been spotted by someone better at regex's than me :-)

The line:

RewriteRule .*/go([^\?]*)(?:\?(.*))? /go.cfm\?path=/go$1&$2 [PT,L,QSA]
Should have been:
RewriteRule ^/go([^\?]*)(?:\?(.*))? /go.cfm\?path=/go$1&$2 [PT,L,QSA]

This forces the regex to match the url from the start - otherwise the following url would match at the second /go and wouldn't work.

With the old rule rewrite this as follows:

While the new rule does what we want at rewrites it as this:

I've updated the original article to use the correct regular expression.

Update - another fix to the regex - see related article

Flexible Friendly URLs for Farcry with Mod rewrite

Posted At : January 7, 2007 6:06 PM 12 Comments

I've known about and used Spike's friendly URL servlet which does a great job of handling friendly URLs for quite a while, but recently I was shown an alternative way by Gav Cooney of Websonic.

This alternative method uses apache's mod_rewrite functionality to dynamically rewrite the url before passing it to ColdFusion. This example uses the exact same format as the FU servlet so it should be possible to drop it into place.

So without further ado - here is what you do.

Put this in the virtual host section of your apache config:

RewriteEngine On
RewriteCond %{REQUEST_METHOD} ^(TRACE|TRACK)
RewriteRule .* - [F]
RewriteRule ^/go([^\?]*)(?:\?(.*))? /go.cfm\?path=/go$1&$2 [PT,L,QSA]

Advantages

  • Simple to change the prefix: i.e. /go/ or use multiple different ones
  • Can add additional parameters
  • Moves the load from java to apache, which is easier to scale

Disadvantages

  • Not available on IIS - which isn't really a disadvantage :-)

Here's and example of some of the flexibility: Multiple working urls which pass a parameter - /ie/ add language=ie and /en/ adds language=en

RewriteRule ^/en([^\?]*)(?:\?(.*))? /go.cfm\?path=/go$1&$2&language=en [PT,L,QSA]
RewriteRule ^/ie([^\?]*)(?:\?(.*))? /go.cfm\?path=/go$1&$2&language=ie [PT,L,QSA]

Hope it helps, and love to hear your thoughts or comments.

Updated - changed regex to always use the start of the url - see related article.

Update 2 - another fix to the regex - see related article