Forum Discussion

COMMS-CORE_1795's avatar
COMMS-CORE_1795
Icon for Nimbostratus rankNimbostratus
Feb 01, 2016

2 actions in the same rule: redirect + load balance

Hi all,

 

I need to make an iRule whose behaviour is:

 

1.- When the web starts with "/a/b" redirect to "/b" AND balance to the pool POOL_x.x.x.x_8090 2.- When the web starts with "/a/" redirect to "/" AND balance to the pool POOL x.x.x.x_8090

 

I managed to do it with 2 irules:

 

First: redirect

 

when HTTP_REQUEST { if { [HTTP::uri] starts_with "/a/b" } { HTTP::redirect "http://[HTTP::host]/b" } elseif { [HTTP::uri] starts_with "/a" } { HTTP::redirect "http://[HTTP::host]/" } }

 

Second: Balance

 

when HTTP_REQUEST { set myURL [HTTP::uri] if { $myURL starts_with "/" } { pool POOL_x.x.x.x_8090 } elseif { $myURL starts_with "/b } { pool POOL_x.x.x.x_8090 } }

 

But I must consolidate in the same iRule,

 

Any ideas?

 

Regards

 

6 Replies

  • Hi Pete,

     

    I´m new in iRules, Can you be more especific? With an example,

     

    Thanks in advanced

     

  • How about this:

    when HTTP_REQUEST {  
        if { [HTTP::path] starts_with "/a/b" } { 
            pool POOL_ABC
            set HTTP::path "/"
        } elseif { [HTTP::path] starts_with "/b } { 
            pool POOL_XYZ
            set HTTP::path "/"
        } 
    }
    
  • By changing the path and selecting which pool to go to you are negating the need for a redirect.

     

    You would only want to use the redirect if you needed to send a traffic to a completely different host that is not hosted on this F5.

     

  • Hi Comms-Core,

    you can chain as many conditions in a single

    [if]
    command by using
    elseif
    or
    else
    conditions. The most restictive conditions are evaluated first followed by the least most restictive conditions.

    Based on your provided requirements, the

    [if]
    command would look like this.

    when HTTP_REQUEST {
         Format the URI tolower to negate the CASE while comparing the requested URIs
        set low_uri [string tolower [HTTP::uri]]
         Evalute the contition step-by-step. Starting with the most restrictive condition...
        if { $low_uri starts_with "/a/b" } then { 
            HTTP::redirect "http://[HTTP::host]/b" 
        } elseif { $low_uri starts_with "/a" } then { 
            HTTP::redirect "http://[HTTP::host]/" 
        } elseif { $low_uri starts_with "/b" } then { 
            pool POOL_1.1.1.1_8090
        } else {
             The else condition will catch everything else (e.g. /*)
            pool POOL_2.2.2.2_8090
        }
    }
    

    Cheers, Kai