Learn F5 Technologies, Get Answers & Share Community Solutions Join DevCentral

Filter by:
  • Solution
  • Technology
Answers

iRule which check uri or path contains

I need to do iRule which check uri or path and send traffic to different pools, and when client try connect to "testweb.com/api/app/1/workstation-params/100" it wiil go to "testweb.com/api/app/workstation-params/100" and pool "pool1"

When client try "/api_aaa" - go to pool "pool2"

When try only "testweb.com/" - go to pool "pool2"

I have irule, but it doesn't work correctly.

when HTTP_REQUEST {
  if { [HTTP::uri] contains "/api/app/1/" } {
  HTTP::path "/api/app/"
  }
  elseif { [HTTP::uri] contains "/api/app/" } { 
        pool pool1
} 
elseif { [HTTP::path] equals "/api_aaa"} {
    pool pool2
} else {
    pool pool2
}
}
0
Rate this Question

Answers to this Question

placeholder+image
USER ACCEPTED ANSWER & F5 ACCEPTED ANSWER

Hi,

You can try a switch case

when HTTP_REQUEST {
    set myuri [HTTP:uri]
    switch -glob [string tolower $myuri]{

    "/api/app/1/" {
        HTTP:path "/api/app/"
        pool pool1
    }
    "/api/app/" { 
        pool pool1
    } 
    "/api_aaa" {
        pool pool2
    } 
    else {
        pool pool2
    }
}
1
placeholder+image
USER ACCEPTED ANSWER & F5 ACCEPTED ANSWER

The HTTP::path "/api/app/" statement in your code is replacing the entire path with the value specified, not just the /api/app/1/ part. As a result, your path is changing from /api/app/1/workstation-params/100, for example, to just /api/app/, and dropping the workstation-params/100 part. To replace just the portion of the path string that needs to change and, more importantly, also select pool1, you could try something like this:

when HTTP_REQUEST {
    if { [string tolower [HTTP::uri]] contains "/api/app/1/" } {
        HTTP::uri [string map { "/api/app/1/" "api/app/" } [string tolower [HTTP::uri]]
        pool pool1
    } elseif { [string tolower [HTTP::uri]] contains "/api/app/" } {
        pool pool1
    } else {
        pool2
    }
}

I don't see the need to check for "/api_aaa" separately as the last elseif and else conditions in your original code both do the same thing which is to select pool2 - but I could be missing something.

0
Comments on this Answer
Comment made 2 months ago by Volodymyr 11

You missed one close-brace. But now i see error: "2: error: [undefined procedure: elseif]"

when HTTP_REQUEST {
    if { [string tolower [HTTP::uri]]  contains "/api/app/1/" } {
        HTTP::uri [string map { "/api/app/1/" "api/app/" }  [string tolower [HTTP::uri]]] } {
        pool pool1
    } elseif { [string tolower [HTTP::uri]] contains "/api/app/" } {
        pool pool1
    } else {
        pool pool2
    }
}

Check for "/api_aaa" need, because it will forward traffic to another pool

0
Comment made 2 months ago by jaikumar_f5 1594

Here, pls use this formatted one, there was just one ] missing

when HTTP_REQUEST {
    if { [string tolower [HTTP::uri]] contains "/api/app/1/" } {
        HTTP::uri [string map { "/api/app/1/" "api/app/" } [string tolower [HTTP::uri]]]
        pool pool1
    } elseif { [string tolower [HTTP::uri]] contains "/api/app/" } {
        pool pool1
    } else {
        pool pool2
    }
}
0
Comment made 2 months ago by crodriguez

Figures I'd miss a bracket. Thanks for keeping me honest!

0