Forum Discussion

Shurton's avatar
Shurton
Icon for Nimbostratus rankNimbostratus
Jun 19, 2020

irule for pool selection based on URI

I lifted this portion of an irule from a bigger one used elsewhere and I can't get the syntax right.

I basically need the URI examined, and if it matches one in the list (test1, test2,test,3) then forward to pool ONPREM, otherwise send to pool CLOUD.

 

 

when HTTP_REQUEST {

   set host [HTTP::host]

   set uri [HTTP::uri]   

 

   switch -glob [string tolower $host] {

      "HOSTNAME" {

         switch -glob [string tolower $uri] {

            "/test1*" -

            "/test*" -

            "/test3*" -

            {

               pool ONPREM

            }   

               else {

            pool CLOUD

            }

 

3 Replies

  • As an alternative, many are unaware of the matches_glob operator, which would be of great use in this case:

    when HTTP_REQUEST {
      if {[HTTP::uri] matches_glob {*test[123]*} } {
        pool ONPREM
      } else { pool CLOUD }
    }

    Not only does it simplify the condition, but it reduces the character count to be stored, and it also saves a few thousand CPU cycles per iteration compared to the other solution.

    Another thing I'd note is the URI is if you want this to be case insensitive, you can either change your glob match for the test characters, or easier, use string tolower around the URI command.

    i featured this question on the You Want Answers?!?! live stream, check it out!

  •  Best option for this would be using LTM Policy. But still you can even try below iRule.

     

     

    when HTTP_REQUEST {

     

    if {([HTTP::uri] contains "test1") || ([HTTP::uri] contains "test2") || ([HTTP::uri] contains "test3")} {

     

      pool ONPREM

    } else {

      pool CLOUD

    }

    }

     

     

    Hope it helps!

    Mayur