URL Variables

Topics: User Forum
Nov 14, 2006 at 5:42 AM
I need to create a rule(s) to take a url such as


and turn it into


However I need this to apply to all .cfm pages regardless of how many variables are passed in the query string. It could range anywhere from 0 to N.
Nov 15, 2006 at 10:41 PM
Just to confirm, you want users to enter "help.cfm?id=234&active=1&orderby=3" in the browser, and have it rewritten to "help.cfm/id/234/active/1/orderby/3/"? I ask because ususally people want to do the reverse.
Nov 16, 2006 at 1:06 AM
I want to things to happen. One I want a url such as test.cfm?variable1=1... rewritten to test.cfm~variable1~1

The second thing is I want

test.cfm~variable1~1 rewritten to test.cfm?variable1=1
Nov 16, 2006 at 1:08 AM
More specifically I want the test.cfm?variable1=1 to redirect to test.cfm~variable1~1 where it is rewritten to test.cfm?variable1=1 so the coldfusion server can intepret it.
Jun 1, 2007 at 7:28 PM
I am trying to do the reverse, to have


rewritten to


for processing by the server. I haven't been able to find the rule to do this anywhere. Can anyone help?

Jun 3, 2007 at 12:20 PM
Edited Jun 3, 2007 at 12:20 PM
Hi Jim,

I've answered your original post.

deltatuk, do I get you correctlly?

1. Client Requests '/test.cfm?variable1=1'
2. Client Redirected to '/test.cfm~variable1~1'
3. URL Rewritten to '/test.cfm?variable1=1'

I'm a little confused why you would want to do this, and I'd suspect if you did implement the rules you would probably get stuck in a loop.

Jun 13, 2007 at 12:53 AM
jimsteele, in general you should not hijack someone else's thread. In general, if people have a new question, they should please create a new thread!

If you want to Rewrite from /help/123/1/3 to /help.cfm?id=123&active=1&orderby=3, then try something like Example 4 in the readme.

   RewriteRule  ^/help/(\d+)/(\d+)/(\d+)   /help.cfm?id=$1&active=$2&orderby=$3

I think what deltatuk is asking is the same, except he wants it to be generalized to allow for N query params. There is no test or example on this currently, I think. But this set of rules ought to do it, iteratively.
# This rule shows how to transform a URL into a query string, piecewise, iteratively.
# In this case, path segments come in pairs.  The first of the pair is the param
# name, the second of the pair is the param value.  So, a pair of path segments in
# the request URL like .../a/b... will result in a rewrite of ?a=b.
# Run the test to see the full results. 
# handle case with no query string .  This rule fires the first time through.
RewriteRule ^/(?!help\.cfm)([^\?\/]+)/([^\?\/]+)/([^\?]+)$ /$3?$1=$2
# handle case with existing query string. This rule fires potentially multiple times.
RewriteRule ^/(?!help\.cfm)([^\?\/]+)/([^\?\/]+)/([^\?]+)\?(.+)$ /$3?$4&$1=$2
# Handle the case with one more pair of segments. This fires once (last).
# It fires when there is an even number of segments. 
RewriteRule ^/(?!help\.cfm)([^\?\/]+)/([^\?\/]+)\?([^\?]+)$ /help.cfm?$3&$1=$2  [L]
# Handle the case with exactly one pair of segments. This fires once (last).
RewriteRule ^/(?!help\.cfm)([^\?\/]+)/([^\?\/]+)$ /help.cfm?$1=$2  [L]
# Handle the edge case, where there is an odd number of segments (invalid input URL)
# This fires once, after all the other pairs have been parsed.  In fact the filter
# cannot know that there is an odd number of segments until it does all the matching.
# So at the end we see, we have one left over segment, and we 
# redirect to a 404 page.
RewriteRule ^/(?!help\.cfm)([^\/\?]+)\?([^\?]+)$ /ResourceNotFound.cfm  [L]
# Handle the edge case where there is only one segment. 
RewriteRule ^/(?!help\.cfm)([^\/\?]+)$ /FoundOnlyOneSegment.cfm  [L]

The results will be something like this:

request Rewrite Result
/id/7 /help.cfm?id=7
/id/7/category/44 /help.cfm?id=7&category=44
/bucket/77/action/refresh/item/92 /help.cfm?bucket=77&action=refresh&item=92
/oneLonelySegment /FoundOnlyOneSegment.cfm
/this/1/URL/2/has/3/a/4/large/5/number/6/of/7/segments/8 /help.cfm?this=1&URL=2&has=3&a=4&large=5&number=6&of=7&segments=8
/this/1/URL/2/has/3/an/4/odd/5/number/6/of/7/segments /ResourceNotFound.cfm