Forum Discussion

rdessert_76127's avatar
rdessert_76127
Icon for Nimbostratus rankNimbostratus
Dec 06, 2012

Using switch vs if to clean up irule

I'm attempting to clean up an irule that currently uses lots of "if" statements and want to use the "switch" statement instead. I've attempted to format the iRule as follows, but I cannot seem to get the syntax correct.

 

Any assistance is much appreciated!

 

 

when HTTP_REQUEST {

 

Redirect to www.site.org

 

switch -glob [string tolower [HTTP::host]] {

 

"site.org" -

 

"site.com" -

 

"www.site.com"

 

{

 

HTTP::respond 301 Location "http://www.site.org[HTTP::uri]"

 

}

 

Match uri patterns and redirect to https

 

switch -glob [string tolower [HTTP::uri]] {

 

"/uri1*" -

 

"/uri2*" -

 

"/uri3*" -

 

"/uri4*" -

 

"/uri5*"

 

{

 

log local0.alert "https redirect:[HTTP::host]-[HTTP::uri]-[HTTP::path]"

 

HTTP::respond 301 Location "https://[HTTP::host][HTTP::uri]"

 

return

 

}

 

Match uri's and send traffic to pool

 

log local0.alert "BEGIN Host:[HTTP::host]-[HTTP::uri]-[HTTP::path]"

 

switch -glob [string tolower [HTTP::uri]] {

 

"/" -

 

"/*" -

 

"/\\?*" -

 

"/uri10/*" -

 

"/uri11/*" -

 

"/uri12/*" -

 

"/uri13/*" -

 

"/uri14/*" -

 

"/uri15*"

 

{

 

log local0.alert "pool1 Host:[HTTP::host]-[HTTP::uri]"

 

pool pool1

 

return

 

}

 

default {

 

pool pool2

 

}

 

}

 

}

 

}

 

4 Replies

  • Looks to me like you are missing a closing curly brace after each of the first two switch statements, and an extra one after the last switch statement. Does this work?

     

     

    when HTTP_REQUEST {

     

    Redirect to www.site.org

     

    switch -glob [string tolower [HTTP::host]] {

     

    "site.org" -

     

    "site.com" -

     

    "www.site.com"

     

    {

     

    HTTP::respond 301 Location "http://www.site.org[HTTP::uri]"

     

    }

     

    }

     

     

    Match uri patterns and redirect to https

     

    switch -glob [string tolower [HTTP::uri]] {

     

    "/uri1*" -

     

    "/uri2*" -

     

    "/uri3*" -

     

    "/uri4*" -

     

    "/uri5*"

     

    {

     

    log local0.alert "https redirect:[HTTP::host]-[HTTP::uri]-[HTTP::path]"

     

    HTTP::respond 301 Location "https://[HTTP::host][HTTP::uri]"

     

    return

     

    }

     

    }

     

     

    Match uri's and send traffic to pool

     

    log local0.alert "BEGIN Host:[HTTP::host]-[HTTP::uri]-[HTTP::path]"

     

    switch -glob [string tolower [HTTP::uri]] {

     

    "/" -

     

    "/*" -

     

    "/\\?*" -

     

    "/uri10/*" -

     

    "/uri11/*" -

     

    "/uri12/*" -

     

    "/uri13/*" -

     

    "/uri14/*" -

     

    "/uri15*"

     

    {

     

    log local0.alert "pool1 Host:[HTTP::host]-[HTTP::uri]"

     

    pool pool1

     

    return

     

    }

     

     

    default {

     

    pool pool2

     

    }

     

    }

     

    }

     

  • That worked perfect! The rule saved successfully. I'm still working on getting proper syntax down :-)

     

     

    Thanks for the help.

     

     

    Rich
  • You should also add a return after this line to ensure that you don't continue evaluating the rule code after sending a redirect:

     

     

    HTTP::respond 301 Location "http://www.site.org[HTTP::uri]"

     

    return

     

     

    Aaron