Forum Discussion

Martijn_Dekkers's avatar
Martijn_Dekkers
Icon for Nimbostratus rankNimbostratus
Jun 15, 2006

Matching wildcards in URI

Hi all, I'm new to iRules and could use some help.

 

 

I am trying to redirect to https if certain portions of the uri match - a sample uri would be http://testserv/test/folder1/folder2/id=1234

 

 

the condition I want to look for is when the uri includes "test" AS WELL AS "id=1234" - the bits in between can change. I have had success looking for just "id=1234" but including the "/test/" is not really working out.

 

 

Thanks a million

 

 

Martijn

5 Replies

  • Hi Martijn,

    You can use a regular expression with matches_regex:

    
    when HTTP_REQUEST {
       if { [HTTP::uri] matches_regex "(?i)^\/test\/.*id\=1234" } {
          log local0. "URI matched regex: [HTTP::uri]"
          pool http_pool
       } else {
          log local0. "URI didn't match regex: [HTTP::uri]"
          pool other_pool
       }
    }

    The regex: (?i)^\/test/\.*id\=1234

    is broken down as:

    (?i) - case insensitive comparison

    ^ - asserts that the following token is the first one in the string

    \/ - literal forward slash

    test - literal

    \/ - literal forward slash

    .* any number of non-line break characters

    id - literal

    \= - literal =

    1234 - literal

    I believe the regex libary is PCRE (Perl Compatible Regular Expression), but someone can correct me if I'm wrong.

    Aaron
  • regex works great, but is a resource hog. You could try:

    
    when HTTP_REQUEST {
      if { ([HTTP::uri] starts_with "/test") and ([HTTP::uri] contains "id=1234") } {
        HTTP::redirect https://your.host.here
        }
    }
  • Hey guys, thanks a lot, this worked great - citizen_elah, can you roll up multiple tests in this? along the lines of

    
     if { ([HTTP::uri] starts_with "/test") and (([HTTP::uri] contains "id=1234") or ([HTTP::uri] contains "id=4567") or ([HTTP::uri] contains "id=8901")) } {

    or would it be bette rto split this up in multiple if statements?

    Thanks again,

    Martijn
  • In the case you will have multiple id=xxxx, I'd build a class:

    
    class allowed_ID {
     "1234"
     "4567"
     "8901"
    }

    Then reference the ID first, but redirect only if the URI begins with /test:

    
    when HTTP_REQUEST {
      if { [matchclass [HTTP::uri] contains "$::allowed_ID"] } {
        if { [HTTP::uri] starts_with "/test" } {
          HTTP::redirect https://your.host.here
        }
      }
    }

    Might check the syntax on my matchclass against other posts in the forum, I haven't personally used it before.