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

Filter by:
  • Solution
  • Technology
Answers

Combining iRules -> switch statements

I have a master iRule which is doing simple redirecting based on http::uri

I would like to add an additional set of rules that tries to do a string match for a specific URI typed in, such as:

http://events.oursite.com/Event.aspx?Event=someevent2010

I cannot, for the life of me, figure out how to integrate this rule into my existing rules, which look like so:

(sorry for the formatting, forums don't like http links)

when HTTP_REQUEST {
   set h [HTTP::host]
set u [HTTP::uri]
switch -glob $h {
"*somedomain1.*"
     {
HTTP::redirect "https://somesite.com/EventOverview.aspx?Event=someevent1"
      return
}
    "*somedomain2.*"
    {
HTTP::redirect "https://anothersite.com/EventOverview.aspx?Event=someevent2"
return
}
"*somedomain3.*"
    {
HTTP::redirect "http://oursite.com"
return
}
}
}


I have tried the following, and for some reason it doesn't seem to catch it.  Can anyone help me with this string match, string to lower problem?

when HTTP_REQUEST {
   set h [HTTP::host]
set u [HTTP::uri]
  switch -glob [string tolower [HTTP::uri]] {
"events.oursite.com/Event.aspx?Event=someevent2010"
   {
HTTP::redirect "http://sub.domain.com/events/some-url-2010/home.aspx"
}
}
switch -glob $h {
"*somedomain1.*"
     {
HTTP::redirect "https://somesite.com/EventOverview.aspx?Event=someevent1"
      return
}
    "*somedomain2.*"
    {
HTTP::redirect "https://anothersite.com/EventOverview.aspx?Event=someevent2"
   return
}
"*somedomain3.*"
    {
HTTP::redirect "http://oursite.com"
return
}
}
}
1
Rate this Question

Answers to this Question

placeholder+image
USER ACCEPTED ANSWER & F5 ACCEPTED ANSWER
Hi Joe,

You can nest switch cases as shown below. I've split out the host and URI check you were trying to do together into separate switch cases. Also, you don't need an explicit return statement in each switch case. Once a switch case matches, no other switch cases in the same statement will be evaluated.


when HTTP_REQUEST {
   # Check the Host header value, set to lowercase
   switch -glob [string tolower [HTTP::host]] {
      "events.oursite.com" {
         # Check the URI, set to lowercase
         switch [string tolower [HTTP::uri]] {
            "/event.aspx?event=someevent2010" {
               HTTP::redirect "https://sub.domain.com/events/some-url-2010/home.aspx"
            }
         }
      }
      "*somedomain1.*" {
         HTTP::redirect "https://somesite.com/EventOverview.aspx?Event=someevent1"
      }
      "*somedomain2.*" {
         HTTP::redirect "https://anothersite.com/EventOverview.aspx?Event=someevent2"
      }
      "*somedomain3.*" {
         HTTP::redirect "https://oursite.com"
      }
   }
}


Aaron
0
placeholder+image
USER ACCEPTED ANSWER & F5 ACCEPTED ANSWER
Awesome - I will try this out right now, thank you for your help!
0
placeholder+image
USER ACCEPTED ANSWER & F5 ACCEPTED ANSWER
hello all,

Am using the same Irule, but i need to extend it ( add a default switch). so that if the website to be used is not specified in the redirection, the connection will be directed back to http.

can any advice of any configuration i can use
0
placeholder+image
USER ACCEPTED ANSWER & F5 ACCEPTED ANSWER
Hi Viper,
In Hoolio's example if the request came from http it will continue to use http if it doesn't match ANY of the switch conditions. That is if you want to go to the same redirected links but intead in HTTP


#Bhattman
0