Forum Discussion

Nandhi's avatar
Nandhi
Icon for Cirrus rankCirrus
Feb 28, 2023

irule not closed with else { }

If the irule statement not ended with else { ] contains multiple ifelse. What will be the result. Whether loop can occur or it just skip the irule if no conditions matched?

8 Replies

  • Nandhi I believe you mean that if the following is configured what will happen. Please note that this only has an if without an else following it.

    if { [HTTP::host] == "example.com" }{
        HTTP::redirect "http://www.example.com/"
    }

     If the rule mentioned was in place traffic would continue on unless of course it matched the exact string of "example.com" but if it didn't it would be directed to whatever default action the VS has configured such as a pool that would receive traffic from the VS. Typically the else statement is used to force a specific behavior if your traffic does not match the if statement.

  • Hi Paul,

    Thanks for the reply, my irule looks like below.

    if { [HTTP::path] == "/" }
    {
    if { [HTTP::host] == "example.com" } { HTTP::redirect "http://www.example.com/"}
    elseif { [HTTP::host] == "example1.com" } { HTTP::redirect "http://www.example1.com/"}
    elseif { [HTTP::host] == "example2.com" } { HTTP::redirect "http://www.example2.com/"}
    }

    There is no else { } statement mentioned to execute if all above not matched. In this case I assume that VS can skip the irule and forward the traffic to default values mentioned in the VS settings. Thanks.

    • Paulius's avatar
      Paulius
      Icon for MVP rankMVP

      Nandhi If this is the only configuration in your iRule then I would use the following iRule assuming the default action is that you would like to send traffic to the pool configured under the virtual server. The reason I prefer this route is because not putting an else makes you depend on the default behavior never changing but if you put the behavior that you want to occur you never have to worry about the default action that should occur if you don't define it.

       

      when CLIENT_ACCEPTED priority 500 {
      
          set DEFAULT_POOL [LB::server pool]
      
      }
      
      when HTTP_REQUEST priority 500 {
      
          set URI [string tolower [HTTP::uri]]
      
          if { ${URI} == "/"} {
              switch --exact [HTTP::host] {
                  "example.com" {
                      HTTP::redirect "http://www.[HTTP::host]/"
                  }
                  "example1.com" {
                      HTTP::redirect "http://www.[HTTP::host]/"
                  }
                  "example2.com" {
                      HTTP::redirect "http://www.[HTTP::host]/"
                  }
                  default {
                      pool ${DEFAULT_POOL}
                  }
              }
          } else {
              pool ${DEFAULT_POOL}
          }
      
      }

       

      • Nandhi's avatar
        Nandhi
        Icon for Cirrus rankCirrus

        Hi Paul,

        Thanks for the update. The irule only used for redirection. The pool members already mapped to the VS as default forward. 

        In the current irule statement there is no else { } added in the script. I am wondering if none of the  statements are matched in the elseif condition whether LB skipped the irule and perform the traffic forward as default or drop the connection. Thanks.

  • Thanks much. So if the irule not contains the default action (else {}) to be performed then it will loop back to the default settings on the virtual server hence not dropping the connection.