I have a framework that I would like all the elements passed to a single controller. Problem is I dont know for sure how many elements will be passed (although it should always be more than 8)
For example: http://me.com/one/two/three/four/five should generate http://me.com/index.cfm?v1=one&v2=two&v3=three&v4=four&v5=five
Is there a short way to do this or would I just need 8 separate rules?
Or, would I need 16 rules? One to handle URL's with the trailing slash (http://me.com/one/) and one to handle the non trailing slash (http://me.com/one)
Sorry, if this is a dumb question, just trying to wrap my head around how to deal with this issue, any help is greatfully appreciated.
May 11, 2007 at 9:37 PM
If you don't mind each parameter being named the same, then you should be able to use the iterative behavior of IIRF to accomplish this. The result will be something like http://me.com/index.cfm?v=one&v=two&v=three&v=four&v=five.
Here's a sample. I use a negative-lookahead - in any case where index.cfm is on the URL, I don't rewrite. In those cases where I have a segmented URL (rule 1), I break it up and take the first segment, and put it onto a querystring param (Adding a ?). at this
point the IIRF engine will re-apply rules. On subsequent segments, rule 2 fires, because there is a question mark already in the URL. Again I take the first segment, and append it with an ampersand in the output string. and again re-apply rules. This keeps
happening until I have one URL segment remaining, when rule 3 will fire, and do the last segment, and prepend with index.cfm.
Rule 4 is just to handle an edge case, where you have a URL with a single segment only.
# This rule shows how to transform a URL into a query string, piecewise, iteratively.
# handle case with no query string . This rule fires the first time through.
RewriteRule ^/(?!index\.cfm)([^\?\/]+)/([^\?]+)$ /$2?v=$1
# handle case with existing query string. This rule fires potentially multiple times.
RewriteRule ^/(?!index\.cfm)([^\?\/]+)/([^\?]+)\?(.+)$ /$2?$3&v=$1
# handle case with one more segment. This fires once (last).
RewriteRule ^/(?!index\.cfm)([^\/]+)\?([^\?]+)$ /index.cfm?$2&v=$1 [L]
# handle edge case, where there is only one segment. This fires once (last).
RewriteRule ^/(?!index.cfm)([^\/]+)$ /index.cfm?v=$1 [L]
When depending on the IIRF iteration behavior, Mind the iteration limit. (check the readme for more info)
Another option is, rather than transforming each url segment to a querystring param, just transform the entire URL to a querystring param. The result in this case would be something like this: http://me.com/index.cfm?v=one/two/three/four/five .
Here's a sample. Again I use a negative-lookahead - only rewrite if the URL does not include index.cfm. Then I just take the entire url and stuff it into a querystring param, named v.
RewriteRule ^/(?!index.cfm)(.+)$ /index.cfm?v=$1