Parsing name/value pairs from a SES URL using Apache mod_rewrite

There are lots of ways to parse the name/value pairs wrapped into so-called SES URLs. Ray has a good post and discussion on the topic at ColdFusionJedi.com. I encountered a situation today where parsing the URL within ColdFusion caused a page with Flash forms (generated by CFFORM) to fail. I assume some sort of path lookup/mismatch issue when the browser requests the CFSWF file. At any rate, moving the parsing into Apache is a fine solution. The following mod_rewrite rules do the trick:

# Split CFML file and path info RewriteCond %{QUERY_STRING} ^$ RewriteCond %{REQUEST_URI} ^(.*)\.cfm(/.+)$ RewriteRule .* %1.cfm?%2 # Parse the first pair RewriteCond %{QUERY_STRING} ^/([^/]+)/([^/]+)(/.+)?$ RewriteRule (.*) $1?%1=%2///%3 # Parse next pair and loop RewriteCond %{QUERY_STRING} ^(.*)////([^/]+)/([^/]*)(/.*)?$ RewriteRule (.*) $1?%1&%2=%3///%4 [N] # Remove lonely sentinel RewriteCond %{QUERY_STRING} ^(.*)///$ RewriteRule (.*) $1?%1 [PT,L]

While testing, I used Charles to replay long URL requests until I had it working correctly. The target CFML page had a CFDUMP of the URL scope to show the final result. Admittedly, this doesn't work for all possible character situations, like embedding an ampersand.

You could use any sentinel you want -- I only used a series of muiltple dashes because it matches the convention used elsewhere.

There are probably many other recipes to solve this problem, but I didn't find one quickly with The Google. If you spot any flaws, be sure to comments. There's one optimization I could make to the ruleset above, but it makes it less human readable.