How to create iRule to forward request depending on context input

For eg. when user type http://abc.com/aa/bb/cc/xx/yy/zz , iRule should redirect to http://abc.com/aa/bb/cc?t1=xx&t2=yy&t3=zz

Explanation of above Example

http://abc.com/aa/bb/cc/xx/yy/zz [ aa,bb,cc are static values and xx,yy,zz are dynamic values (user provided inputs ) ] http://abc.com/aa/bb/cc?t1=xx&t2=yy&t3=zz [ &,t1,=,xx,t2,t3, are static)

Expected result: User inputs xx,yy,zz should sit in the appropriate output context path ie. xx should sit after t1= , yy after t2= , zz after t3=

Try something as follows. If you want to use 302 as a redirect method, make sure you also use the

TCP::close

close command after

event disable
when HTTP_REQUEST {

    if { [HTTP::uri] starts_with "/aa/bb/cc" } {
      set PREFIX_STATIC "/aa/bb/cc"
      log local0. "Original URI: [HTTP::uri]"
      set XX_VAL [ getfield [HTTP::uri] "/" 5]
      set YY_VAL [ getfield [HTTP::uri] "/" 6]
      set ZZ_VAL [ getfield [HTTP::uri] "/" 7]
      log local0. "XX_VAL = $XX_VAL | YY_VAL = $YY_VAL | ZZ_VAL = $ZZ_VAL"
      log local0. "Redirect destination: [HTTP::host]$PREFIX_STATIC?t1=$XX_VAL&t2=$YY_VAL&t3=$ZZ_VAL"
      HTTP::respond 301 Location "[HTTP::host]$PREFIX_STATIC?t1=$XX_VAL&t2=$YY_VAL&t3=$ZZ_VAL"
      event disable
    }
}

Oct 14 10:12:08 local/tmm info tmm[15625]: Rule irule_asd-test : Original URI: /aa/bb/cc/f3/f4/f5

Oct 14 10:12:08 local/tmm info tmm[15625]: Rule irule_asd-test : XX_VAL = f3 | YY_VAL = f4 | ZZ_VAL = f5

Oct 14 10:12:08 local/tmm info tmm[15625]: Rule irule_asd-test : Redirect destination: www.abc.com/aa/bb/cc/?t1=f3&t2=f4&t3=f5

Try something as follows. If you want to use 302 as a redirect method, make sure you also use the

TCP::close

close command after

event disable
when HTTP_REQUEST {

    if { [HTTP::uri] starts_with "/aa/bb/cc" } {
      set PREFIX_STATIC "/aa/bb/cc"
      log local0. "Original URI: [HTTP::uri]"
      set XX_VAL [ getfield [HTTP::uri] "/" 5]
      set YY_VAL [ getfield [HTTP::uri] "/" 6]
      set ZZ_VAL [ getfield [HTTP::uri] "/" 7]
      log local0. "XX_VAL = $XX_VAL | YY_VAL = $YY_VAL | ZZ_VAL = $ZZ_VAL"
      log local0. "Redirect destination: [HTTP::host]$PREFIX_STATIC?t1=$XX_VAL&t2=$YY_VAL&t3=$ZZ_VAL"
      HTTP::respond 301 Location "[HTTP::host]$PREFIX_STATIC?t1=$XX_VAL&t2=$YY_VAL&t3=$ZZ_VAL"
      event disable
    }
}

Oct 14 10:12:08 local/tmm info tmm[15625]: Rule irule_asd-test : Original URI: /aa/bb/cc/f3/f4/f5

Oct 14 10:12:08 local/tmm info tmm[15625]: Rule irule_asd-test : XX_VAL = f3 | YY_VAL = f4 | ZZ_VAL = f5

Oct 14 10:12:08 local/tmm info tmm[15625]: Rule irule_asd-test : Redirect destination: www.abc.com/aa/bb/cc/?t1=f3&t2=f4&t3=f5

Thanks Hannes, working perfect .

We are facing some caching issue , but I think it is related to app side , need to fine tune.

Thanks again..

I didn’t get what for 5 , 6 and 7 in the script , what if I put some other value?

You can use any values of any lengths you want. (e.g www.abc.com/aa/bb/cc/hhhhhhhhhhh/ssssssss/llllllllllll would work exactly the same) For example: set XX_VAL [ getfield [HTTP::uri] “/” 5] - returns anything entered in between fourth and fifth slash symbol as a string

Working fine for “/” (slash) separation in URI . What if the separation is “-”(dash) ? For eg: if the url is http://abc.com/aa/bb/cc/xx-yy-zz iRule should redirect to http://abc.com/aa/bb/cc?t1=xx&t2=yy&t3=zz

Working fine for “/” (slash) separation in URI . What if the separation is “-”(dash) ? For eg: if the url is http://abc.com/aa/bb/cc/xx-yy-zz iRule should redirect to http://abc.com/aa/bb/cc?t1=xx&t2=yy&t3=zz

Keep everything the same, except the 3 lines below. (Untested solution) set XX_VAL [ getfield [HTTP::uri] “-” 0] set YY_VAL [ getfield [HTTP::uri] “-” 1] set ZZ_VAL [ getfield [HTTP::uri] “-” 2]

Keep everything the same, except the 3 lines below. (Untested solution) set XX_VAL [ getfield [HTTP::uri] “-” 0] set YY_VAL [ getfield [HTTP::uri] “-” 1] set ZZ_VAL [ getfield [HTTP::uri] “-” 2]