Variable Length URI Lookup

Problem this snippet solves:

This iRule uses findclass to perform class lookup for URI's of varying path length, returning the value for the longest path matched. This example uses the lookup value for pool selection, but of course could be modified to send a redirect perform some other appropriate action. This rule should only be used on v9. If using v10 or v11, it's recommended to use "class match" instead of findclass.

Code :

when HTTP_REQUEST {
  set lookup [HTTP::path]
  set i 1
  while { $i > 0 }{
    set myPool [findclass $lookup $::myPools " "]
    if {$myPool != ""}{
      pool $myPool
      # log local0. "Path: [HTTP::path]  Pool: $myPool"
      break
    } else {
      set i [string last "/" [string range $lookup 0 [expr [string length $lookup]-2]]]
      set lookup [string range $lookup 0 $i]
    }
  }
}

### Required Class: ###
# (Note all paths are represented twice, once with a trailing slash and once without. 
# That format is critical for proper functioning of the iRule above if legal URI's for which
#  the match must succeed may be submitted without the trailing slash.)

class myPools  {
   "/this pool6"
   "/this/ pool6"
   "/this/is pool5"
   "/this/is/ pool5"
   "/this/is/my pool4"
   "/this/is/my/ pool4"
   "/this/is/my/path pool3"
   "/this/is/my/path/ pool3"
   "/this/is/my/path/ok pool2"
   "/this/is/my/path/ok/ pool2"
   "/this/is/my/path/ok/file.html pool1"
}

Tested this on version:

9.0
Published Jan 30, 2015
Version 1.0

Was this article helpful?