Replace a query string variable value

This should be pretty straight forward but am having problems finding a way to change a query string variable value. basically I have an encoded url in the query string that I need to change to a different encoded url. I tried doing a string map on HTTP:uri, which didnt seem to work for values.

If anyone can help, would be most appreciated

Thanks!

Could you post a (sanitized, if necessary) copy of one of the URLs and what you need to change in it? A regex could probably do it, though there may be more efficient methods.

yeah the url is horrendous but the query variable in question is

TARGET=-SM-http%3a%2f%2frbweb--dev%2egerbc%2erbcmtg%2ecom%2fcar%2f

So I want to change this “TARGET” variable to the value

TARGET=-SM-http%3a%2f%2f172%2e16%2e20%2e83%2fcar%2f

I’ve been playing around a little with this:

when HTTP_REQUEST {  
  set TARGET [URI::query [HTTP::uri] "-SM-http%3a%2f%2f172%2e16%2e20%2e83%2fcar%2f"]  
  }

…bleh, never mind. I thought I had something with this, but was seeing an artifact of my local apache instance.

Give this a shot:

when HTTP_REQUEST { 
   HTTP::uri [string map {"rbweb--dev%2egerbc%2erbcmtg%2ecom" "172%2e16%2e20%2e83"} [HTTP::uri]] 
 }

When you said it didn’t work for values, what did you mean? I tested this out with a test URL of

http://myserver/foo/bar?TARGET=-SM-http%3A%2F%2Frbweb--dev.gerbc.rbcmtg.com%2Fcar%2F

and it changed it and sent it to the backend server as

http://myserver/foo/bar?TARGET=-SM-http%3A%2F%2F172.16.20.83%2Fcar%2F

Of course, if you need to expand this, you could put the relevant URL’s in a class and do a lookup and then a replace based on a match in the class but you only specified that you had one url to change.

Hope this helps…

-Joe

string map…beautiful. filing that one away for sure.

Thanks Joe. I tried the string map before but was using -nocase option…would that make it not work for some reason?

Anyway its working for me now, thanks much.

John

Hi John,

I know the nocase flag isn’t supported with switch statements as Joe pointed out in this post (Click here). But -nocase should work for string map:

when RULE_INIT { 
  
    log local0. "[string map {find replace} "search in string: FIND replace"]" 
    log local0. "[string map -nocase {find replace} "search in string: FIND replace"]" 
  
 }

Log output:

Rule : search in string: FIND replace

Rule : search in string: replace replace

Maybe it was down to using a variable with curly braces? This would not work because the curly braces force a literal interpretation of the search/replace fields:

... 
    set find "abc" 
    set replace "xyz" 
  
    log local0. "[string map {$find $replace} "search in string: abc"]" 
 ...

Log output:

Rule : search in string: abc

You could use quotes instead to allow for variable expansion:

set find "abc" 
    set replace "xyz" 
  
    log local0. "[string map "$find $replace" "search in string: abc"]"

Rule : search in string: xyz

Aaron

Ha… actually I should read the posts I’m referencing. Joe stated in that post that the -nocase flag isn’t supported for any command. I think this has changed in recent versions or was only true for the switch command. It works for string map in 9.4.5 and probably earlier versions (Click here - 9.1.0).

If you test with the -nocase option, do you get a TCL error in /var/log/ltm either when you try to save the rule or when you hit that portion of the code in a request?

Aaron

btw, thanks for all the help guys appreciate it.