Forum Discussion
Hi Eirikn,
I've to second Cjuniors opinion to use [class] instead of [array] for this specific task. The reason for this is, that an [array] can't be easily crawled in a $STRING starts_with [array names] fashion. Well, using a [foreach] loop will work, but its far away from being ideal... 😉
But if you still want to stick with this approach, then you may want to take a look to the iRule below.
It resolves your question be using a [string map -nocase [list $web_uri ""] [HTTP::uri]] and has some additional performance optimizations included (e.g. using RULE_INIT to define the [array]'s not on every request, removed the trailing / slashes from redirect locations, changed the code to use [array names], removed the duplicated execution of [string tolower [HTTP::host]] and finally combined the [if $host_header] statements.
BTW: I duno if a starts_with operator for every $host_header would be sufficient, too?
when RULE_INIT {
unset -nocomplain static::pools
array set static::pools {
sliders.test.example.no pool_example_test
bee.test.example.no pool_example_test
tinypower.test.example.no pool_example_test
tbank.test.example.no pool_example_test
bank.test.example.no pool_example_test
womvalley.test.example.no pool_example_test
redakto- pool_example_redaktor
priceapi1-sitecore1.test.example.no pool_example_redaktor
}
unset -nocomplain static::redirects
array set static::redirects {
/caf https://banktest.portal.no/dummy-text_to_show-something-web
/insurance http://test.dummy.customer.no/example
/cfs https://cfs-ws-customer.vendor.no
}
}
when HTTP_REQUEST {
foreach web_uri [array names static::redirects] {
if { [string tolower [HTTP::uri]] starts_with "$web_uri" } then {
HTTP::respond 302 noserver Location "$static::redirects($web_uri)[string map -nocase [list $web_uri ""] [HTTP::uri]]"
return
}
}
foreach host_header [array names static::pools] {
set low_host [string tolower [HTTP::host]]
if { ($low_host equals $host_header) or
($low_host starts_with $host_header) } then {
pool $static::pools($host_header)
return
}
}
HTTP::respond 200 Content "This site does not exist"
}
Cheers, Kai