Forum Discussion

giorgio_32761's avatar
giorgio_32761
Icon for Nimbostratus rankNimbostratus
Dec 21, 2018

URI Manipulation

Hi, I would like to extract part of a URI, from the 5th "/" to the end, for example:

 

/string1/string2/string3/string4/something/seomethingelse/

 

should become:

 

/something/somethingelse

 

I cannot use one of the string as delimiter, since they change, but for sure they are always 4

 

I found that this works, but I wonder if there is some solution more "efficient":

 

set uri [HTTP::uri]

 

set fields [split $uri "/"]

 

set num_fields [llength $fields]

 

set str1 [getfield [HTTP::uri] "/" 2]

 

set str2 [getfield [HTTP::uri] "/" 3]

 

set str3 [getfield [HTTP::uri] "/" 4]

 

set str4 [getfield [HTTP::uri] "/" 5] set newuri ""

 

set finaluri ""

 

for {set x 5} {$x<$num_fields} {incr x} {

 

set newuri /[lindex $fields $x]

 

set finaluri $finaluri$newuri

 

Thanks

 

1 Reply

  • Giorgio,

    Here is an iRule I wrote to be a more efficient solution. Based on what I understand, it seems like you simply want to eliminate the first four items of the URI. I found a simple TCL command that will allow you to replace the undesired elements, effectively removing them. This will allow you to create the URI you want in a bit more optimized way. If there is a modification you want or questions about the iRule, I am sure I can help.

    when HTTP_REQUEST
    {
        set uri_list [split [HTTP::uri] "/"]
        if {[llength $uri_list] > 5}
        {
            set new_uri_list [lreplace $uri_list 0 4 ""]
            HTTP::uri [join $new_uri_list "/"]
        }
    }