Forum Discussion

minche22_258903's avatar
minche22_258903
Icon for Nimbostratus rankNimbostratus
Apr 13, 2016

irule to remove url ending

hello, i need to write an irule to redirect user to the same url but without the file at the end. for example if user request http://www.example.com/test.cgi?parm1=test&parm2=test2 or http://www.example.com/bla/bla/test.cgi?parm1=test&parm2=test2

 

i need to redirect him to: http://www.example.com/?parm1=test&parm2=test or http://www.example.com/bla/bla/?parm1=test&parm2=test2

 

so just removing the test.cgi and keep the parameters.

 

thank you:)

 

9 Replies

  • Is the part to be removed always the same (in that case "test.cgi")?
  • yes, i wrote this: when HTTP_REQUEST { if { [HTTP::path] ends_with "cgi" && [HTTP::host] starts_with "example.com"} { HTTP::redirect "http://[HTTP::host]/?[HTTP::query]" } } and still wont work
  • Try with splitting the HTTP Path in sections and removing the last one, see the following irule as an example:

    when HTTP_REQUEST {
     set path_list [split [HTTP::path] "/"]
      set len [llength $path_list]
        HTTP::path [join [lreplace $path_list $len-1 $len-1 ""] "/"]
    }
    
  • This, maybe?

     

    when HTTP_REQUEST {

     

    if { [HTTP::path] equals "/test.cgi" } {

     

    set queryString [HTTP::query]

     

    HTTP::redirect http://[HTTP::host]/?$queryString

     

    } else {

     

    pool whatever_pool

     

    }

     

    }

     

    • minche22_258903's avatar
      minche22_258903
      Icon for Nimbostratus rankNimbostratus
      i don't see why i need an else statement also, but your solution wont work on the second condition: http://www.example.com/bla/bla/test.cgi it will erase the full path....
    • ekaleido's avatar
      ekaleido
      Icon for Cirrus rankCirrus
      True, I overlooked that piece. Change "equals" to "contains" to get through that. The else is just there to handle paths without test.cgi, but I suppose you could get rid of it.
  • This, maybe?

     

    when HTTP_REQUEST {

     

    if { [HTTP::path] equals "/test.cgi" } {

     

    set queryString [HTTP::query]

     

    HTTP::redirect http://[HTTP::host]/?$queryString

     

    } else {

     

    pool whatever_pool

     

    }

     

    }

     

    • minche22_258903's avatar
      minche22_258903
      Icon for Nimbostratus rankNimbostratus
      i don't see why i need an else statement also, but your solution wont work on the second condition: http://www.example.com/bla/bla/test.cgi it will erase the full path....
    • ekaleido_26616's avatar
      ekaleido_26616
      Icon for Cirrocumulus rankCirrocumulus
      True, I overlooked that piece. Change "equals" to "contains" to get through that. The else is just there to handle paths without test.cgi, but I suppose you could get rid of it.