Graham's .plan:

Apache Redirect Madness

Coldfusion lacks the integration with Apache that I've come to expect from mod_perl and mason, so when our web developers came to me asking for prettyurls (search engine safe URLs), I was somewhat hesitant. Fortunately, a few hours of thinking and tinkering, and mod_rewrite saved the day.

Behold:
RewriteEngine on

# check if the whole path exists as a file or directory, bail if so
RewriteCond %{REQUEST_URI} ^(/.+?)/?$
RewriteCond %{DOCUMENT_ROOT}%1 !-f
RewriteCond %{DOCUMENT_ROOT}%1 !-d
# cut the first portion off, then check if .cfm exists
RewriteCond %{REQUEST_URI} ^/([^\/]+)(/(.*?))?/?$
RewriteCond %{DOCUMENT_ROOT}/%1.cfm -f
# proxy rewrite to the real url
RewriteRule ^.* /%1.cfm/?prettyurl=/%3&%{QUERY_STRING} [PT,NS]

What this accomplishes is the transparent (to the user and the backend) rewriting of http://www.example.com/foo/bar into http://www.example.com/foo.cfm?prettyurl=/bar.

One nice detail (worth the complexity, in my opinion) is that regardless of what the user inputs, the backend code will receive a consistent string, according to user expectations. That is, requests for /foo/bar/ and /foo/bar will both feed /foo/bar into the prettyurl parameter.

It also doesn't have any effect on directories or files that exist, so you can mix static content with automagically generated content to no ill effects. Additionally, it's more efficient than even having .htaccess files enabled, for most cases.