Forum Discussion

C_D_18583's avatar
C_D_18583
Icon for Nimbostratus rankNimbostratus
Aug 04, 2005

IRule and Error Checking

Please See comments below

 

 

when HTTP_REQUEST {

 

if { [HTTP::uri] starts_with "/channel/" } {

 

pool QA1

 

}

 

elseif {[HTTP::uri] starts_with "/performance/" } {

 

pool QA2

 

}

 

elseif { [HTTP::uri] starts_with "/lead/" } {

 

pool QA3

 

}

 

 

Please review the irule

 

I want the user to end the URI with /. If this is not done, I need

 

to redirect the request with the "/" at the end.

 

The statement below does NOT

 

work. What is the best way to write this.

 

 

elseif { not[HTTP::uri] ends_with "/" } {

 

HTTP::redirect "https://[HTTP::host][HTTP::uri]/"

 

}

 

 

else {

 

discard

 

}

 

}

3 Replies

  • It seems that the ends_with operator isn't returning a boolean value (or at least that's what is being inserted into the logs)

    can't use non-numeric string as operand of "!"

    while executing "if { ! [HTTP::uri] ends_with "/" }..."

    maybe unRuleY can chime in as to why. Since it's not returning a boolean so you can't use the not operator with it.

    But, you aren't out of luck, you can always use the built-in TCL string commands (Click here) to manipulate strings

    Give this a try...

    when HTTP_REQUEST { 
      if { [HTTP::uri] starts_with "/channel/" } {
        pool QA1
      } elseif {[HTTP::uri] starts_with "/performance/" } {
        pool QA2
      } elseif { [HTTP::uri] starts_with "/lead/" } {
        pool QA3
      } elseif { [string index [HTTP::uri] end] ne "/" } { 
        HTTP::redirect "https://[HTTP::host][HTTP::uri]/"
      } else {
        discard
      }
    }

    Now, you didn't state this, but your rule will only do the redirect if the above "starts_with" comparisions don't pass. If you want this to occur even on those uri's then you might want to move the ends_with piece to the beginning of the loop. Also, keep in mind, that some webservers won't like appending slashes to file name requests (ie. /dir/somefile.htm).

    -Joe
  • unRuleY_95363's avatar
    unRuleY_95363
    Historic F5 Account
    This is caused by something called operator precedence. The "not" operator (an alias for "!") is of higher precendence than starts_with, ends_with or equals. So, what's happening is that the not is being applied to the [HTTP::uri] operand and then the result of that is being checked whether it ends_with "/". Clearly not what you want.

    Try adding some parenthesis to tell Tcl want should happen first:

    
    elseif { not ( [HTTP::uri] ends_with "/" ) } {
       HTTP::redirect "https://[HTTP::host][HTTP::uri]/"
    }
  • Thanks Joe , Your solution works! I will also try the other suggestion from unRuleY