Remove part of querystring

I want to remove specific key=value pair from the querystring if present

-If my uri is /myuri?mykey=myvalue or /myuri?otherkey=othervalue&mykey=myvalue -I want to rewrite the uri such that I remove the mykey=myvalue from the uri in the HTTP_REQUEST event -such that the url is /myuri or /myuri?otherkey=othervalue

Please advise.

Thanks Rajesh

Just one approach;-

when HTTP_REQUEST {
    if {[URI::query [HTTP::uri]] contains "mykey="} {
        set newq ""
        foreach param [split [URI::query [HTTP::uri]] "&"] {
            if {!($param starts_with "mykey=")} {
                     Build up new query parameter string
                append newq $param
            }
             Add new query portion to path
            HTTP::uri "[HTTP::path]$newq"
        }
    }

Thank you IheartF5! This did the job.

I actually made a little mistake - I forgot to add in the ? if there is still a query string remaining it should say

if {$newq ne ""} {
        HTTP::uri "[HTTP::path]?$newq"
   } else {
        HTTP::uri [HTTP::path]
   }

Thank you again IheartF5!

For everyone benefit, here I wanted to share the final version. From the above script we failed to put & in between each key=value pairs. Here is the working script:

when HTTP_REQUEST { set newq “”

foreach param [split [URI::query [HTTP::uri]] “&”] { if {!($param contains “mykey=” )} { if {$newq eq “”} { append newq $param } else { append newq “&$param” } } } Add new query portion to path if {$newq ne “”} { HTTP::uri “[HTTP::path]?$newq” } else { HTTP::uri [HTTP::path] } }

you may also try HTTP::query. HTTP::query writing data is introduced in 11.5.0.

ID385615 - RFE: Make HTTP::query iRule command support writing data

e.g.

config

[root@ve11a:Active:In Sync] config  tmsh list ltm rule qux
ltm rule qux {
    when RULE_INIT {
  set static::qry_name "mykey"
  set static::qry_value "myvalue"
}
when HTTP_REQUEST {
  log local0. "origin=[HTTP::uri]"

  set tmp [string map [list $static::qry_name=[URI::query [HTTP::uri] $static::qry_name] ""] [HTTP::query]]
  set tmp [string map {&& &} $tmp]
  set tmp [string trimleft $tmp &]
  HTTP::query $tmp
  HTTP::uri [string trimright [HTTP::uri] ?&]

  log local0. "new=[HTTP::uri]"
}
}

 test

[root@ve11a:Active:In Sync] config  tail -f /var/log/ltm
Jun  8 11:09:21 ve11a info tmm[9801]: Rule /Common/qux : origin=/myuri
Jun  8 11:09:21 ve11a info tmm[9801]: Rule /Common/qux : new=/myuri

Jun  8 11:09:38 ve11a info tmm1[9801]: Rule /Common/qux : origin=/myuri?mykey=myvalue
Jun  8 11:09:38 ve11a info tmm1[9801]: Rule /Common/qux : new=/myuri

Jun  8 11:09:44 ve11a info tmm[9801]: Rule /Common/qux : origin=/myuri?mykey=myvalue&otherkey=othervalue
Jun  8 11:09:44 ve11a info tmm[9801]: Rule /Common/qux : new=/myuri?otherkey=othervalue

Jun  8 11:09:54 ve11a info tmm1[9801]: Rule /Common/qux : origin=/myuri?otherkey=othervalue&mykey=myvalue
Jun  8 11:09:54 ve11a info tmm1[9801]: Rule /Common/qux : new=/myuri?otherkey=othervalue

Jun  8 11:10:03 ve11a info tmm[9801]: Rule /Common/qux : origin=/myuri?otherkey=othervalue&mykey=myvalue&anotherkey=anothervalue
Jun  8 11:10:03 ve11a info tmm[9801]: Rule /Common/qux : new=/myuri?otherkey=othervalue&anotherkey=anothervalue