Irule with Regex's

Hi all,

I’m new to this forum so be nice :slight_smile: hahaha..

Just playing around to setup a IRule to rewrite a request URL to a server. What needs to be done is that the client style get request should be something like get /internet/chevvy/impala/2009 and the server style should be like /internet/iets.asp/merk=chevy&type=impala&model=2009.

So i created something like this which works like a charm:

when HTTP_REQUEST {

set uri [HTTP::uri]

regexp {/internet/(.*)/(.*)/(.*)$} $uri → m1 m2 m3

log local0. “regexp: m1=$m1, m2=$m2, m3=$m3”

log local0. “$uri”

set fields [split $uri “/”]

log local0. “split: m1=[lindex $fields 2], m2=[lindex $fields 3], m3=[lindex $fields 4]”

HTTP::uri “/iets.asp\?merk=${m1}&model=${m2}&type=${m3}”

log local0. “new URI: [HTTP::uri]”

}

However, the client should also be able just to acces /internet/chevvy without anything behing it, so i tried to repeat the above into:

when HTTP_REQUEST {

if { [HTTP::uri] matches_regex "^/internet/\w$ " } {

set uri [HTTP::uri]

regexp {/internet/(.*)$} $uri → m1

log local0. “regexp: m1=$m1”

log local0. “$uri”

set fields [split $uri “/”]

log local0. “split: m1=[lindex $fields 2]”

HTTP::uri “/iets.asp\?merk=${m1}”

log local0. “new URI: [HTTP::uri]”

}

if { [HTTP::uri] matches_regex {/internet/\w/\w$ }} {

set uri [HTTP::uri]

regexp {/internet/(.*)/(.*)/(.*)$} $uri → m1 m2

log local0. “regexp: m1=$m1, m2=$m2”

log local0. “$uri”

set fields [split $uri “/”]

log local0. “split: m1=[lindex $fields 2], m2=[lindex $fields 3],”

HTTP::uri “/iets.asp\?merk=${m1}&model=${m2}”

log local0. “new URI: [HTTP::uri]”

}

if { [HTTP::uri] matches_regex {/internet/\w/\w/\w$ }} {

set uri [HTTP::uri]

regexp {/internet/(.*)/(.*)/(.*)$} $uri → m1 m2 m3

log local0. “regexp: m1=$m1, m2=$m2, m3=$m3”

log local0. “$uri”

set fields [split $uri “/”]

log local0. “split: m1=[lindex $fields 2], m2=[lindex $fields 3], m3=[lindex $fields 4]”

HTTP::uri “/iets.asp\?merk=${m1}&model=${m2}&type=${m3}”

log local0. “new URI: [HTTP::uri]”

}

else {

log local0. “no match on [HTTP::uri]”

}

}

However, it only hits at the no match escape else at the bottom. If i use the hashes () to disable the first IF, just 1 parameter works like a charm, same for the second and third IF statements.

I’m loosing it here. Running on v11.1. Should be almost there :).

Your help is appriciated!

Pascal

Maybe try rewritng like so, avoiding regex altogether:

- Split URI to parts

- If part1 is not “internet”, return, do nothing.

- init new URI as /iets.asp?

- if part two is present

-- Append merk param

- if part two is present

-- Append merk param

- if part3 is present

…