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

Filter by:
  • Solution
  • Technology
Answers

iRule works but getting browser error, help!

need an iRule that looks for trafficui and gives pool but if it does not see trafficui it looks for the hostname and redirects

it seems to work but i get errors on the "test1.com" links. better way to do this?

thanks for any help

when HTTP_REQUEST {
if { [HTTP::uri] starts_with "/TrafficUI" } {
pool Testy
}
elseif { [HTTP::host] starts_with "test1" } {
HTTP::redirect "http://www.test1.com/TrafficUI/mscui/page.aspx?cid=1&ptid=5"
}
elseif { [HTTP::host] starts_with "www.test1" } {
HTTP::redirect "http://www.test1.com/TrafficUI//mscui/page.aspx?cid=1&ptid=5"
}
elseif { [HTTP::host] starts_with "test2" } {
HTTP::redirect "http://www.test2.com/TrafficUI/mscui/page.aspx?cid=1&ptid=10"
}
elseif { [HTTP::host] starts_with "www.test2" } {
HTTP::redirect "http://www.test2.com/TrafficUI//mscui/page.aspx?cid=1&ptid=10"
}
elseif { [HTTP::host] starts_with "test" } {
HTTP::redirect "http://www.google.com/"
}
}
0
Rate this Question

Answers to this Question

placeholder+image
USER ACCEPTED ANSWER & F5 ACCEPTED ANSWER
hi,
instead of using if - elseif statements you should use the "switch" command. For reference check the wiki here: Click here
As I read in another post, the switch works faster than the if-then-else command set.


schwiddy
0
placeholder+image
USER ACCEPTED ANSWER & F5 ACCEPTED ANSWER
At first glance, it looks good to me (besides the double slash in the "www.test1" and www.test2 redirects - unless this is what you intended). What kind of errors are you getting?

Here's an example of using a switch statement for your reference (with test1* and www.test1* redirecting to the same uri).

when HTTP_REQUEST {
if { [HTTP::uri] starts_with "/TrafficUI" } {
pool Testy
} else {
switch -glob { [HTTP::host] } {
"test1*" -
"www.test1*" {
HTTP::redirect "http://www.test1.com/TrafficUI/mscui/page.aspx?cid=1&ptid=5"
}
"test2*" -
"www.test2*" {
HTTP::redirect "http://www.test2.com/TrafficUI/mscui/page.aspx?cid=1&ptid=10"
}
"test*" {
HTTP::redirect "http://www.google.com/"
}
}
}
}
0
placeholder+image
USER ACCEPTED ANSWER & F5 ACCEPTED ANSWER
thanks for the help

This is the error:

Line: 4
Char: 1
Error: Syntax Error
Code: 0

also, i used your example and it never seems to get to the redirect if i use the domain with no URI

also, is there a way to have an irule look for the host, then see what the uri is and act upon it

thanks again
0
placeholder+image
USER ACCEPTED ANSWER & F5 ACCEPTED ANSWER
you think this is better than the if, elseif?

when HTTP_REQUEST {
if {[string tolower [HTTP::uri]] starts_with "/trafficui" } {
pool PoolOne
else {
switch -glob "[HTTP::host][HTTP::uri]" {
"*test1.com/" {
HTTP::redirect "http://www.test1.com/trafficui/mscui/page.aspx?cid=1&ptid=10"
}
"*test2.com/" {
HTTP::redirect "http://www.test2.com/trafficui/mscui/page.aspx?cid=1&ptid=10"
}
default {
HTTP::redirect "http://test3.com/TrafficUI/mscui/page.aspx?cid=1&ptid=4&utcoffset=5"
}
}
}
}

what i'd like to do with this one is *.test1.com/ (starts with anything other then traffcui) HTTP::redirect blahblah

is it possible?
0
placeholder+image
USER ACCEPTED ANSWER & F5 ACCEPTED ANSWER
Sorry about the syntax error. The extra curlies around the switch string is invalid.

Looking at your script here.

1. You'll need a end curly before the else
2. by adding the HTTP::uri into the check and comparing against host + slash, means that only requests without a URI will be matched (ie http://test1.com/ will, but http://test1.com/foo/bar will not). If that is what you intended then that's fine. But you specified after your rule tha tyou wanted anything other than starting with trafficui to be redirected and for that your iRule won't work.
3. I'm not sure if "*test1.com" will match "test1.com" or not (not sure if a wildcard will match nothing or if it requires at least one character).


Here's an updated one (that should work ). Comments in the iRule...

when HTTP_REQUEST {
if {[string tolower [HTTP::uri]] starts_with "/trafficui" } {
pool PoolOne
} else {
switch -glob [string tolower [HTTP::host]] {
"test1.com" -
"*.test1.com" {
HTTP::redirect "http://www.test1.com/trafficui/mscui/page.aspx?cid=1&ptid=10"
}
"test2.com" -
"*.test2.com" {
HTTP::redirect "http://www.test2.com/trafficui/mscui/page.aspx?cid=1&ptid=10"
}
default {
HTTP::redirect "http://test3.com/TrafficUI/mscui/page.aspx?cid=1&ptid=4&utcoffset=5"
}
}
}
}


Good luck!

-Joe
0
placeholder+image
USER ACCEPTED ANSWER & F5 ACCEPTED ANSWER
that seems to work better, thanks for all the help
0
placeholder+image
USER ACCEPTED ANSWER & F5 ACCEPTED ANSWER
is there a better way to get this done? i tried a switch - glob [HTTP::uri] and then switch - glob [HTTP:host] but it just skipped right over the uri section

this is working but does not look very scalable

when HTTP_REQUEST {
if {[string tolower [HTTP::uri]] starts_with "/trafficui" } {
pool trafficui
} elseif {[string tolower [HTTP::uri]] starts_with "/testuri" } {
HTTP::redirect "http://www.testuri.com/TrafficUI/mscui/Page.aspx?pgid=598"
} else {
switch -glob [string tolower [HTTP::host]] {
"test1.com" -
"*.test1.com" {
HTTP::redirect "http://wwv.test1.com/trafficui/mscui/page.aspx?cid=1&ptid=10"
}
"test2.com" -
"*.test2.com" {
HTTP::redirect "http://www.test2.com/TrafficUI/mscui/page.aspx?cid=1&ptid=5"
}
default {
HTTP::redirect "http://www.test3.com/TrafficUI/mscui/page.aspx?cid=1&ptid=4&utcoffset=5"
}
}
}
}

much appreciated
0