Mod_rewrite hell
I’ve been living in mod_rewrite hell today. There are lots of different ways to get clean urls in ColdFusion, none of them I particularly like. The classic trick, is just to append all your variables to the end of the file like so:
/twitter.cfm/var1/value1/var2/value2
Then you use OnRequest in your Application.cfc to manage the list of variables with a little bit regex. This works, and I was using this in my Twitter app, but I hate having twitter.cfm in the path. It just looks ugly. So, I spent a while today, trying to work out how to do the same thing with mod_rewrite.
After a bit of searching, and a lot of reading, this is what I came up with. There are better ways to do this, but this works for this app, and I know that I will not be passing more than 3 variables back through the quesry string.
RewriteCond %{REQUEST_URI} ^.*..*$ [NC]
RewriteRule ^.*$ - [L,NC,QSA]
RewriteRule ^twitter/(.*)/(.*)/(.*)/(.*)/(.*)/(.*)$ twitter.cfm?$1=$2&$3=$4&$5=$6 [L,NC,QSA]
RewriteRule ^twitter/(.*)/(.*)/(.*)/(.*)$ twitter.cfm?$1=$2&$3=$4 [L,NC,QSA]
RewriteRule ^twitter/(.*)/(.*)$ twitter.cfm?$1=$2 [L,NC,QSA]
RewriteRule ^twitter/$ twitter.cfm [L,NC,QSA]
RewriteRule ^twitter$ twitter.cfm [L,NC,QSA]
There are several ways to try and make this dynamic, and I did work on a perl script that used RewriteMap to build out the string. However, it keep doing something strange, and I just gave up on it. This type of approach has a 9 variable limit, due to the way that mod_rewrite sees $1 and $10. However, it works for me, and it’s working damn well, I might add.
In case your wondering about the items at the end of the rewriterule, they mean: last rule, case insensitive, and append query string. The rest is all just simple regex.
Go play around with it: http://themadness.org/twitter/







