Forum Discussion

Brian_Gupta_115's avatar
Brian_Gupta_115
Icon for Nimbostratus rankNimbostratus
Jan 12, 2005

Converting a 4.5 rule to an iRule

I have the following 4..5 rule that I need to convert to an irule... Can someone please help me?

 

 

Thanks,

 

Brian

 

 

if (http_uri contains "HR" or http_uri contains "hr") {

 

redirect to "https://pshr.txxx.com/hr"

 

}

 

else if (http_uri contains "SELFSERVICE" or http_uri contains "selfservice") {

 

redirect to "https://pshr.txxx.com/selfservice"

 

}

 

else if (http_uri contains "FINTI" or http_uri contains "finti") {

 

redirect to "https://psfin.txxx.com/finti"

 

}

 

else if (http_uri contains "FINTL" or http_uri contains "fintl") {

 

redirect to "https://psfin.txxx.com/fintl"

 

}

 

else if (http_uri contains "FINSPC" or http_uri contains "finspc") {

 

redirect to "https://psfin.txxx.com/finspc"

 

}

 

else {

 

discard

 

}

 

  • unRuleY_95363's avatar
    unRuleY_95363
    Historic F5 Account
    Brian,

    I would rather you take a stab at this yourself. It's the best way to learn the new syntax. Here are some rules that will make the conversion easier:

    a) http_uri is now [HTTP::uri].

    b) Tcl syntax requires that you use {} instead of () around the expression in an if statement (ie, instead of "if (...) {...}" you would do "if {...} {...}".

    c) You will need to pick an event for the rule to be evaluated under. Since your 4.5 rule is primarily based on the HTTP uri, then the event HTTP_REQUEST would be most suitable.

    Here is a basic template with some examples of what you might want to do:

      
      rule my_redirect {  
         when HTTP_REQUEST {  
            if { [HTTP::uri] contains "HR" or [HTTP::uri] contains "hr" } {  
               redirect to "https://pshr.txxx.com/hr"  
            } elseif { [HTTP::uri] contains "SELFSERVICE" or [HTTP::uri] contains "selfservice") {   
               redirect to "https://pshr.txxx.com/selfservice"   
            }  more elseif's may following here...  
         }  
      }  
      

    I also noticed you are often doing a comparison for both upper and lowercase versions of the string. In 9.x, this can be simplified like so:

      
      rule my_redirect {  
         when HTTP_REQUEST {  
            set my_uri [string tolower [HTTP::uri]]  
            if { $my_uri contains "hr" } {  
               redirect to "https://pshr.txxx.com/hr"  
            } elseif { $my_uri contains "selfservice") {   
               redirect to "https://pshr.txxx.com/selfservice"   
            }  more elseif's may following here...  
         }  
      }  
      

    Good luck, and be sure to post back if you run into any problems.

  • Your examples where very helpful... My final rule:

     

     

    when HTTP_REQUEST {

     

    set my_uri [string tolower [HTTP::uri]]

     

    if { $my_uri contains "hr" } {

     

    redirect to "https://pshr.txxx.com/hr"

     

    } elseif { $my_uri contains "selfservice" } {

     

    redirect to "https://pshr.txxx.com/selfservice"

     

    } elseif { $my_uri contains "finti" } {

     

    redirect to "https://psfin.txxx.com/finti"

     

    } elseif { $my_uri contains "fintl" } {

     

    redirect to "https://psfin.txxx.com/fintl"

     

    } elseif { $my_uri contains "finspc" } {

     

    redirect to "https://psfin.txxx.com/finspc"

     

    }

     

    }