Forum Discussion

kridsana's avatar
kridsana
Icon for Cirrocumulus rankCirrocumulus
Apr 29, 2015

Simplify irule (rather than if ,elseif, elseif ...)

Hi

Is there anyway to simplify or condense this irule?

when HTTP_REQUEST
if {[HTTP::uri] starts_with "/atm/"
            or [HTTP::uri] contains "/ammcontent"
            or [HTTP::uri] contains "/htmlsite"
            or [HTTP::uri] contains "/publichtml"
            or [HTTP::uri] contains "/crmform"
            or [HTTP::uri] contains "/distsite"
            or [HTTP::uri] contains "/wttw"
            or [HTTP::uri] contains "/rules"
            or [HTTP::uri] contains "/attributeweb"
            or [HTTP::uri] contains "/contentweb"  }
                   {    pool atm_HTTP } 

Thank you

8 Replies

  • we got many kind of irule. if we can simplify or condense it , it may help with much performance
  • And what problem if we using many "if" instead of if+elseif

     

    like,

     

    if {aaa}

     

    if {bbb}

     

    if {ccc}

     

  • I think you want something like:

    when HTTP_REQUEST {

    switch -glob [string tolower [HTTP::host]] {
    
        "/atm" {
            pool atm_HTTP
        }
        "/ammcontent*" {
            pool atm_HTTP
        }
    }
    

    }

    You can then future proof it by redirecting based on URI and adding new lines easily.

  • giltjr's avatar
    giltjr
    Icon for Nimbostratus rankNimbostratus
        switch -glob [string tolower [HTTP::uri]] {
        "/ammcontent*" -
        "/htmlsite*" -
        "/publichtml*" -
        "/crmform*" -
        "/distsite*" -
        "/wttw*" -
        "/rules*" -
        "/attributeweb*" -
        "/contentweb*" } {pool atm_HTTP }
        default {pool default_pool }
    }
    
  • Can I do like this?

    when HTTP_REQUEST
    if {[HTTP::uri] starts_with "/atm/"
                or [class match [HTTP::uri] contains my_uri_datagroup] }
                      {    pool atm_HTTP } 
    

    and my_uri_datagroup is include with all those uri "/.."

  • Hello Teepan,

     

    I hope this will work for you. Have you tried this ?

     

  • Arie's avatar
    Arie
    Icon for Altostratus rankAltostratus

    I would recommend converting the URL to lowercase before doing the comparison. Just make sure you always enter the values you want to check for in lowercase.

    So, instead of:

    if {[HTTP::uri] starts_with "/atm/"

    [class match [HTTP::uri] contains my_uri_datagroup] }

    You would use:

    if {[string tolower[HTTP::uri]] starts_with "/atm/"

    [class match [string tolower [HTTP::uri]] contains my_uri_datagroup] }

  • For scale-ability in the future if you ever needed to send traffic to other pools I would recommend creating your string data group with the URIs you want to match with values for the corresponding pool and use an iRule like this. I would also recommend setting the default pool on the virtual server itself.

    when HTTP_REQUEST {
        if {[string tolower [HTTP::uri]] starts_with "/atm/"}{
            pool atm_HTTP
        }
        elseif {[class match [string tolower [HTTP::uri]] contains "/Common/my_uri_datagroup"]}{
            pool [class match -value [string tolower [HTTP::uri]] contains "/Common/my_uri_datagroup"]
        }
    }