Forum Discussion

gbunting's avatar
gbunting
Icon for Nimbostratus rankNimbostratus
Jun 18, 2013

Need help creating iRule to do redirects based on uri

Hi,

 

 

I need to create an iRule that will match this url:

 

 

http://mywebsite.com/SEC+TEST/{x}/{y}.htm

 

 

where {x} and {y} can be anything. This should redirect to:

 

http://myotherwebsite.com/folder1/folder2/KB/redirect.aspx?id={y}

 

 

where {y} is obtained from the original uri.

 

 

I know I can key this off of the if uri begins with SEC+TEST, but I'm not sure how to grab {Y}. Can someon help me figure out the best way to do this?

 

 

Thanks,

 

 

Glen

 

7 Replies

  • Here is just ONE way:

    
    when HTTP_REQUEST {
    if { [string tolower [HTTP::uri]] starts_with "/sec+test/" } {
    catch {
    set index [lindex [split [findstr [string tolower [HTTP::uri]] "/sec+test/" 10 ".htm"] "/"] 1]
    if { $index ne "" } {
    HTTP::respond 302 Location "http://myotherwebsite.com/folder1/folder2/KB/redirect.aspx?id=$index"
    }
    }
    }
    }
    

  • another one. 🙂

    [root@ve10:Active] config  b rule myrule list
    rule myrule {
       when HTTP_REQUEST {
      if { [HTTP::uri] starts_with "/SEC+TEST/" } {
        if { [scan [HTTP::uri] {/SEC+TEST/%*[^/]/%[^.].htm} y] == 1 } {
          HTTP::redirect "http://myotherwebsite.com/folder1/folder2/KB/redirect.aspx?id=$y"
        }
      }
    }
    }
    
    [root@ve10:Active] config  curl -I http://172.28.19.252/SEC+TEST/987654321abc/xyz123.htm
    HTTP/1.0 302 Found
    Location: http://myotherwebsite.com/folder1/folder2/KB/redirect.aspx?id=xyz123
    Server: BigIP
    Connection: Keep-Alive
    Content-Length: 0
    
    
  • Kevin,

     

     

    Thanks for your response. I believe I understand what the rule is doing for the most part. Can you explian what the 10 does in:

     

     

    lindex [split [findstr [string tolower [HTTP::uri]] "/sec+test/" 10 ".htm"] "/"] 1]

     

     

    I want to make sure I understand things correctly.

     

     

    Thanks,

     

     

    Glen

     

  • Nitass,

     

     

    Thanks for your response. This looks like it works as expected. If you don't mind (I need to learn regex much better) can you explain what exactly you are doing with this:

     

     

    {/SEC+TEST/%*[^/]/%[^.].htm} y]

     

     

    Thanks,

     

     

    Glen

     

     

     

  • lindex [split [findstr [string tolower [HTTP::uri]] "/sec+test/" 10 ".htm"] "/"] 1]

    [findstr [string tolower [HTTP::uri]] "/sec+test/" 10 ".htm"] - finds the part of the string that STARTS WITH "/sec+test/", skips 10 characters (the length of "/sec+test/"), and ends before ".htm". If the URI was "/sec+test/foo/bar.htm", then this would return "foo/bar".

    [split [findstr [string tolower [HTTP::uri]] "/sec+test/" 10 ".htm"] "/"] - splits the returned string into a list, based on the delimiter character "/". in the above string this would produce a list of two values: "foo" and "bar".

    [lindex [split [findstr [string tolower [HTTP::uri]] "/sec+test/" 10 ".htm"] "/"] 1] - list extracts the second element from the previously returned list. Lists start with a 0 (zero) index, so the second element is 1. This then returns "bar".

    While findstr is an efficient and simple way to extract text from a given string, I must concede that Nitass' scan example is more elegant. If I may Nitass:

    
    {/SEC+TEST/%*[^/]/%[^.].htm}
    

    The percent sign in the TCL scan command is a "conversion specifier". If you include the * star character after the % it means that you DO NOT want to include the next values in the scan. So %*[^/] means to ignore the next set of values that do not (^ means NOT) contain a "/" character - so don't include the value after "/SEC+TEST/" and before the next "/". The next % character and brackets means to capture the next set of values that do not contain a "." period character, so everything after the last "/" and before ".htm".

    Nice example, Nitass.

  • Kevin,

     

     

    Thanks for the explanations. I think I understand both methods better now.

     

     

    Thanks,

     

     

    Glen