how do I parse a URI value into a variable

Hi,

I would like to “switch” features base on the URI startwith to switch to a defined procedure.

The probem I head is [HTTP::URI] return the whole URL value which is a various length, but I only want to switch base on the first “/xxx/” . How do I use iRule to process this action like following:

Parse “/abc/cde/xyz.jpg” and get the first path value into a Variable to achieve $firstpath == “/abc” .

so that I can use “switch” function to do follow:

switch [string tolower $firstpath ] {

/abc { pool www }

/cde {

HTTP::header insert Header1 domain2

HTTP::header replace Host www.domain.com

HTTP::uri “/domain2[HTTP::uri]”

pool www

}

default { discard }

}

Thanks.

Billy

Hi,

There are two possible methods, either split the uri to get just the first directory, or use glob pattern matching (this is probably the most extensible)…

switch -glob [string tolower [HTTP::uri] ] {

“/abc*” { pool www }

“/cde*” {

HTTP::header insert Header1 domain2

HTTP::header replace Host www.domain.com

HTTP::uri “/domain2[HTTP::uri]”

pool www

}

default { discard }

}

If you do want to get just the first/base directory name you could use something like [getfield [HTTP::uri] “/” 2] - this would get the second field from the uri, using a field seperator of “/”. So you’d get something like “abc” or “cde”, you might to add a leading slash (/) back when you want to use it.

It’s also possible you would need to do further processing to remove parameters in the uri (eg /abc?var=value, or /abc;jsessionid=x).

Another alternative would be to use the subtr command to retrieve a know number of characters (if your base directories all have just 3 character names) - [substr [HTTP::uri] 0 4]

Rob.

thx Rob.

Spot on Rob, thanks for responding.

Coincidentally, I just published a tech tip yesterday covering the “switch -glob” solution: Switch Gone Wild: Using Wildcards with the Tcl “switch” command (Click here)

and also answered another post about grabbing the first directory name using getfield:

Click here

/deb