Forum Discussion

Richard__Harlan's avatar
Richard__Harlan
Historic F5 Account
May 24, 2006

nested if statements

Working on a simple iRule to redirect traffic from certain uris to our siteunavailable page. The main problem is on certain paged we do not want to redirect to a domain name but to an IP address. We use this logic in other rules and it work find but in this one the if statement is always coming back as true. I was hoping someone could catch what we are missing.

 

 

when HTTP_REQUEST {

 

if { [HTTP::uri] matches_regex "^/servlet/mike" or

 

[HTTP::uri] matches_regex "^/servlet/chad" } {

 

if {[string first -nocase "deere" [HTTP::host]]} {

 

log "deere [HTTP::host]"

 

redirect to "http://unavailable.deere.com/index.pl?SiteName=unavailable.deere.com"

 

} else {

 

log "no deere [HTTP::host]"

 

redirect to "http://x.x.x.x/index.pl?SiteName=unavailable.com"

 

}

 

}

 

log "fell thru [HTTP::host]"

 

}

 

 

No matter what the [HTTP::host] is it always catches on the if statement even if [HTTP::host] equals an IP address. Thanks
  • The "string first" command will return the index of the found string or -1 if it's not found. So valid values or -1 (for not found) and 0 to len-1 if the string is found. If you are converting this to a boolean value for the if condition, then the only thing that would fail would be a value of 0 (or when the string starts with your substring). I don't think this is what you want. I'd specifically check that the return value is != -1.

    Also, you are using matches_regex when you don't need to. Since you are just looking for the start of the string, the "starts_with" operator is much more efficient. You should avoid regular expressions unless there is no alternative.

    Give this a shot:

    when HTTP_REQUEST {
      if { [HTTP::uri] starts_with "/servlet/mike" or
           [HTTP::uri] starts_with "/servlet/chad" } {
        if {-1 != [string first -nocase "deere" [HTTP::host]]} {
          log "deere [HTTP::host]"
          redirect to "http://unavailable.deere.com/index.pl?SiteName=unavailable.deere.com"
        } else {
          log "no deere [HTTP::host]"
          redirect to "http://x.x.x.x/index.pl?SiteName=unavailable.com"
        }
      }
      log "fell thru [HTTP::host]"
    }

    You also might need a "string tolower" in there for the HTTP::uri check...

    -Joe