# Remove part of querystring

**URL:** <https://community.f5.com/t/remove-part-of-querystring/20159>\
**Category:** Technical Forums\
**Tags:** design, irules, devops, application-delivery\
**Created:** [April 8, 2014, 9:19pm UTC](https://community.f5.com/t/remove-part-of-querystring/20159 "2014-04-08T21:19:03Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rajesh\_A\_142089](https://avatars.discourse-cdn.com/v4/letter/r/3da27b/32.png) [@Rajesh\_A\_142089](https://community.f5.com/u/Rajesh_A_142089)\
**Post date:** [April 8, 2014, 9:19pm UTC](https://community.f5.com/t/remove-part-of-querystring/20159/1 "2014-04-08T21:19:03Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![IheartF5\_45022](https://avatars.discourse-cdn.com/v4/letter/i/3be4f8/32.png) [@IheartF5\_45022](https://community.f5.com/u/IheartF5_45022)\
**Post date:** [April 9, 2014, 12:28pm UTC](https://community.f5.com/t/remove-part-of-querystring/20159/2 "2014-04-09T12:28:45Z")

</div>

Just one approach;-

```tcl
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"
        }
    }

```

---

<div class="post-metadata">

**Author:** ![Rajesh\_A\_142089](https://avatars.discourse-cdn.com/v4/letter/r/3da27b/32.png) [@Rajesh\_A\_142089](https://community.f5.com/u/Rajesh_A_142089)\
**Post date:** [April 9, 2014, 9:31pm UTC](https://community.f5.com/t/remove-part-of-querystring/20159/3 "2014-04-09T21:31:54Z")

</div>

Thank you IheartF5! This did the job.

---

<div class="post-metadata">

**Author:** ![IheartF5\_45022](https://avatars.discourse-cdn.com/v4/letter/i/3be4f8/32.png) [@IheartF5\_45022](https://community.f5.com/u/IheartF5_45022)\
**Post date:** [April 10, 2014, 12:04am UTC](https://community.f5.com/t/remove-part-of-querystring/20159/4 "2014-04-10T00:04:32Z")

</div>

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

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

```

---

<div class="post-metadata">

**Author:** ![Rajesh\_A\_142089](https://avatars.discourse-cdn.com/v4/letter/r/3da27b/32.png) [@Rajesh\_A\_142089](https://community.f5.com/u/Rajesh_A_142089)\
**Post date:** [April 22, 2014, 9:32pm UTC](https://community.f5.com/t/remove-part-of-querystring/20159/5 "2014-04-22T21:32:46Z")

</div>

Thank you again IheartF5!

---

<div class="post-metadata">

**Author:** ![Rajesh\_A\_142089](https://avatars.discourse-cdn.com/v4/letter/r/3da27b/32.png) [@Rajesh\_A\_142089](https://community.f5.com/u/Rajesh_A_142089)\
**Post date:** [June 6, 2014, 10:42pm UTC](https://community.f5.com/t/remove-part-of-querystring/20159/6 "2014-06-06T22:42:42Z")

</div>

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] } }

---

<div class="post-metadata">

**Author:** ![nitass](https://d1p9zq3aats0t8.cloudfront.net/user_avatar/community.f5.com/nitass/32/2270_2.png) [@nitass](https://community.f5.com/u/nitass)\
**Post date:** [June 8, 2014, 6:11pm UTC](https://community.f5.com/t/remove-part-of-querystring/20159/7 "2014-06-08T18:11:08Z")

</div>

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.

```tcl
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

```
