Forum Discussion

jcline's avatar
jcline
Icon for Nimbostratus rankNimbostratus
Jun 14, 2013

Need help making Irule more efficient

Below is a sample of what is running. Each country/culture has two seperate if statements because we don't want to disrupt flow to other items in those folders. We are in the middle of switching from our old CMS to a new one and need to keep parts of both CMSes alive for now so we are usign iRules to route for 20 different country home pages on 3 different server pools. We have mostly used "equals" but there are one of two instances of "contains" mixed in.

 

 

Any advice on how to improve the structure and perfomance would be appreciated.

 

FYI I am a Sys Admin not a programmer.

 

Thanks in advance.

 

John

 

 

when HTTP_REQUEST {

 

if { [string tolower [HTTP::uri]] equals "/" }{

 

 

HTTP::redirect "http://mysite.com/home/"

 

}

 

 

if { [string tolower [HTTP::uri]] equals "/gb/eng/default.asp" }{

 

 

HTTP::redirect "http://[HTTP::host]/home/en-gb/"

 

}

 

if { [string tolower [HTTP::uri]] equals "/gb/eng/" }{

 

 

HTTP::redirect "http://[HTTP::host]/home/en-gb/"

 

}

 

 

if { [string tolower [HTTP::uri]] equals "/fr/fra/default.asp" }{

 

 

HTTP::redirect "http://[HTTP::host]/home/fr-fr/"

 

}

 

 

if { [string tolower [HTTP::uri]] equals "/fr/fra/" }{

 

 

HTTP::redirect "http://[HTTP::host]/home/fr-fr/"

 

}

 

 

if { [string tolower [HTTP::uri]] contains "/home/admin-tools/" }{

 

 

HTTP::redirect "http://[HTTP::host]/home/"

 

}

 

 

}

 

2 Replies

  • At a minimum you should probably use an if/elseif structure to avoid unnecessary evaluations. Also, for everything except your single "contains" conditional, you could use a switch context, which is generally faster.

    
    when HTTP_REQUEST {
    if { [string tolower [HTTP::uri]] contains "/home/admin-tools/" } {
    HTTP::redirect "http://[HTTP::host]/home/"
    } else {
    switch [string tolower [HTTP::uri]] {
    "/" { HTTP::redirect "http://mysite.com/home/" }
    "/gb/eng/default.asp" -
    "/gb/eng/" { HTTP::redirect "http://[HTTP::host]/en-gb/" }
    "/fr/fra/default.asp" -
    "/fr/fra/" { HTTP::redirect "http://[HTTP::host]/fr-fr/" }
    }
    }
    }
    

    Given that you have 20 countries to check for, and presumably 40 conditions, it may also make sense to throw everything into a data group for easier management:

    Example data group (my_country_datagroup)

    "/" := "/home/"

    "/gb/eng/default.asp" := "/en-gb/"

    "/gb/eng/" := "/en-gb/"

    "/fr/fra/default.asp" := "/fr-fr/"

    "/fr/fra/" := "/fr-fr/"

    Example iRule:

    when HTTP_REQUEST {

    if { [string tolower [HTTP::uri]] contains "/home/admin-tools/" } {

    HTTP::redirect "http://[HTTP::host]/home/"

    } else {

    if { [class match [string tolower [HTTP::uri]] equals my_country_datagroup] } {

    HTTP::redirect "http://[HTTP::host][class match -value [string tolower [HTTP::uri]] equals my_country_datagroup]"

    }

    }

    }

  • jcline's avatar
    jcline
    Icon for Nimbostratus rankNimbostratus

    Thanks!

     

    That is exactly what I was looking for. I just didn't know how to put it together.