URL Query String Rewrite

Topics: Developer Forum, Project Management Forum, User Forum
Jun 1, 2007 at 6:38 PM
I am trying to have the URL (for example)


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 10:54 AM
Edited Jun 3, 2007 at 11:27 AM
RewriteRule ^/(help.cfm)/(.*)/(.*)/(.*)/(.*)/(.*)/(.*)/$ /$1?$2=$3&$4=$5&$6=$7 [I]

Although you might have difficulty if you want something a bit more generic.
Jun 3, 2007 at 8:01 PM
Thanks Ady!

The url rewrite seems to be working properly, but none of my images are showing up. Also, my linked stylesheet isn't being applied. I have read that in some cases you must include a <base href="xxxx"> tag on your page to cover this. But is there any way around it with just a rewrite rule to get the images and stylesheet showing up?

Jun 4, 2007 at 12:30 AM
All images, style sheets, and any linked scripts, should be relative to the root and not absolute...

Wrong: <img src="http://www.mysite.com/images/some_img.jpg" />

Right: <img src="/images/some_img.jpg" />

Also, if your querystring always has ID, ACTIVE, and ORDERBY in it and all you care about are their values then your rule could look like:

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

Just an idea...
Jun 4, 2007 at 3:38 AM
Thank you very much for the help!

One last thing: I am dumping the converted URL variables to my test page just to make sure that they are being passed correctly, and no URL variables are being passed! When I test the page with something like:

index.cfm/var1/value1/var2/value2/ etc.

I would hope to see it converted to index.cfm?var1=value1&var2=value2 and the URL variables var1 and var2 present on the page, but when I output those variables, the page throws an error that they do not exist.

Any ideas as to why the URL variables aren't there?
Jun 4, 2007 at 10:31 AM
Edited Jun 4, 2007 at 11:05 AM
Hi Jim,

If you've copied the rule exactly then it only applies to help.cfm.

The rule is also specific to 3 querystring variables. If you need more you'll need to create more rules.

The following rule applies to all files with a .cfm extension, from 1 to 5 querystring variables.

RewriteRule ^/(.*)\.cfm/(.*)/(.*)/(.*)/(.*)/(.*)/(.*)/(.*)/(.*)/(.*)/(.*)/$ /$1.cfm?$2=$3&$4=$5&$6=$7&$8=$9&$10=$11 [I,L]
RewriteRule ^/(.*)\.cfm/(.*)/(.*)/(.*)/(.*)/(.*)/(.*)/(.*)/(.*)/$ /$1.cfm?$2=$3&$4=$5&$6=$7&$8=$9 [I,L]
RewriteRule ^/(.*)\.cfm/(.*)/(.*)/(.*)/(.*)/(.*)/(.*)/$ /$1.cfm?$2=$3&$4=$5&$6=$7 [I,L]
RewriteRule ^/(.*)\.cfm/(.*)/(.*)/(.*)/(.*)/$ /$1.cfm?$2=$3&$4=$5 [I,L]
RewriteRule ^/(.*)\.cfm/(.*)/(.*)/$ /$1.cfm?$2=$3 [I,L]

Edit: I've updated the order in which the rules are applied, because it's important that the rule with the most amount of querystring variables is processed first.