Forum Discussion

dp_119903's avatar
dp_119903
Icon for Cirrostratus rankCirrostratus
Nov 17, 2015
Solved

Help with RegEx

I am having some difficulty forming the correct syntax (or regex) for an irule.

 

I need to block some specific URI's if they come from a specific data group. The logic works, but the regex doesn't.

 

For example. I need to block any URI that has /admin or /admin/ or /user or /user/ BUT (and this is where it's not working) if the URI= /user-experience then it needs to work.

 

I've used REGEX that looks like

 

elseif {([HTTP::uri] matches_regex "^(/admin/|/user/|/?q=admin|/?q=admin|/?q=user|/?q=user) and ([class match [IP::client_addr] equals block_ip])} {

 

And I've used a bunch of different "starts_with" and "equals" but strangely enough whenever I block "user" it blocks user-experience. Even if I use the "equals" URI. Which I don't understand b/c /user-experience doesn't equal /user.

 

I'm sure there is something simple here...any help would be appreciated.

 

Thanks in advance

 

  • regex in iRule equals bad news. Try something like this:

    elseif { [class match [IP::client_addr] equals block_ip] }{
        switch -glob [string tolower [HTTP::uri]] {
            "/user-experience*" { return }
            "/user*" -
            "/admin*" {
                reject
            }
            default { return }
        }
    }
    

5 Replies

  • regex in iRule equals bad news. Try something like this:

    elseif { [class match [IP::client_addr] equals block_ip] }{
        switch -glob [string tolower [HTTP::uri]] {
            "/user-experience*" { return }
            "/user*" -
            "/admin*" {
                reject
            }
            default { return }
        }
    }
    
  • regex in iRule equals bad news. Try something like this:

    elseif { [class match [IP::client_addr] equals block_ip] }{
        switch -glob [string tolower [HTTP::uri]] {
            "/user-experience*" { return }
            "/user*" -
            "/admin*" {
                reject
            }
            default { return }
        }
    }
    
  • I did this and it worked...I'll give the above a shot.

    when HTTP_REQUEST { 
     if { not ( [class match [IP::client_addr] equals first_data_group] ) } {
      reject
     } elseif {([HTTP::uri] starts_with "/admin") and ([class match [IP::client_addr] equals second_data_group])} { 
        reject
     } elseif {([HTTP::uri] equals "/user") and ([class match [IP::client_addr] equals second_data_group])} { 
        reject
     } elseif {([HTTP::uri] equals "/user/") and ([class match [IP::client_addr] equals second_data_group])} {
        reject
     } elseif {([HTTP::uri] contains "/?q=admin"|"/?q=user") and ([class match [IP::client_addr] equals second_data_group])} {
        reject
     }
    }