Forum Discussion

Joe_Pipitone's avatar
Joe_Pipitone
Icon for Nimbostratus rankNimbostratus
Aug 04, 2010

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
     } 
   } 
 }

4 Replies

  • 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
  • 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
  • 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