Forum Discussion

Piotr_Pozna_ski's avatar
Piotr_Pozna_ski
Icon for Nimbostratus rankNimbostratus
Apr 24, 2015

Best way to rewrite uri to pass path segment as part of query string?

Hello.

I need URI's matching pattern

/prefix//
where
is of form
[a-z]{1,}
to be passed to the internal host as
/
with
id
as a part of query string
id=
.

For example:

/prefix/foo/
->
/?id=foo
/prefix/foo/path
->
/path?id=foo
/prefix/foo/path?
->
/path?id=foo
/prefix/foo/path?p=7
->
/path?p=7&id=foo

What would be the best way to achieve this?

Best regards, Piotr

2 Replies

  • I haven't tested this yet, but you could try somthing like this.

    when HTTP_REQUEST {
        if {[string tolower [URI::path [HTTP::path] 2 2]] matches_regex "[a-z]{1,}"}{
            HTTP::path "/[URI::path [HTTP::path] 3]"
            if {not ([HTTP::query] equals "")}{
                HTTP::query "[HTTP::query]&id=[URI::path [HTTP::path] 2 2]"
            }
            else {
                HTTP::query "id=[URI::path [HTTP::path] 2 2]"
            }
        }
    }
    
  • e.g.

     irule
    
    [root@ve11c:Active:In Sync] config  tmsh list ltm rule qux
    ltm rule qux {
        when HTTP_REQUEST {
      set uri [HTTP::uri]
      if { [scan $uri {/%*[^/]/%[^/]%s} id rest] == 2 } {
        set rest [string trimright $rest "?"]
        if { $rest contains "?" } {
          set newuri "${rest}&id=${id}"
        } else {
          set newuri "${rest}?id=${id}"
        }
        HTTP::uri $newuri
      }
    }
    when HTTP_REQUEST priority 1000 {
      log local0. "$uri > [HTTP::uri]"
    }
    }
    
     test
    
    [root@ve11c:Active:In Sync] config  tail -f /var/log/ltm
    Apr 24 22:50:02 ve11c info tmm1[5649]: Rule /Common/qux : /prefix/foo/ > /?id=foo
    Apr 24 22:50:10 ve11c info tmm[5649]: Rule /Common/qux : /prefix/foo/path > /path?id=foo
    Apr 24 22:50:16 ve11c info tmm1[5649]: Rule /Common/qux : /prefix/foo/path? > /path?id=foo
    Apr 24 22:50:22 ve11c info tmm[5649]: Rule /Common/qux : /prefix/foo/path?p=7 > /path?p=7&id=foo