For more information regarding the security incident at F5, the actions we are taking to address it, and our ongoing efforts to protect our customers, click here.

Forum Discussion

TNY_122436's avatar
TNY_122436
Icon for Nimbostratus rankNimbostratus
Dec 23, 2013

Redirect iRule with URI redirection and pool distribution

Hi,

 

I have an iRule that redirects URI containing certain syntax. I want to add a switch -glob statement so that the group of URI's that 'starts_with' the /page1, /page2, on so forth gets sent to a certain pool else defaults to pool green. I put in:

 

Code
switch -glob [HTTP::uri] starts_with

But that statement above returns me errors. I assume that can't be done or has to be done in some other format? Any help is greatly appreciated. Here's my existing code:

 

Code
when HTTP_REQUEST {
if {[HTTP::uri] contains "/read.pdf"} { 
       HTTP::redirect "https://www.main.com/menu" }

if {[HTTP::uri] starts_with "/Mypersonal/formula"} { 
       HTTP::respond 301 Location "https://www.main.com/colors.html" }  

switch -glob [HTTP::uri] {

"/page1" -
    "/page2" -
    "/page3" -
    "/index" -
    "/start" { pool orange }
    default { pool green }
}
}

Code

3 Replies

  • Please try this:

    when HTTP_REQUEST {
        if { [HTTP::uri] contains "/read.pdf" } { 
            HTTP::redirect "https://www.main.com/menu" 
        } elseif { [HTTP::uri] starts_with "/Mypersonal/formula" } { 
            HTTP::respond 301 Location "https://www.main.com/colors.html" 
        } else {  
            switch -glob [HTTP::uri] {
                "/page1*" -
                "/page2*" -
                "/page3*" -
                "/index*" -
                "/start*" { 
                    pool orange 
                }
                default { 
                    pool green 
                }
            }
        }
    

    The -glob option allows you to use wildcards in the conditions, so a "starts_with" in a switch -glob would be a URI followed by a start "*".

  • Seems like there might be a syntax somewhere that is causing error when I compile the iRule. This is the error I get:

     

    Code
    01070151:3: Rule [/Common/test_redierct] error: 
    line 1: [parse error: missing close-brace] [{
    if { [HTTP::uri] contains "/read.pdf" } { 
    HTTP::redirect "https://www.main.com/menu" 
    } elseif { [HTTP::uri] starts_with "/Mypersonal/formula" } { 
    HTTP::respond 301 Location "https://www.main.com/colors.html" 
    } else { 
    switch -glob [HTTP::uri] {
    "/page1*" -
    "/page2*" -
    "/page3*" -
    "/index*" -
    "/start*" { 
    pool orange 
    }
    default { 
    pool green 
    }
    }
    }]
    line 2: [command is not valid in the current scope] [if { [HTTP::uri] contains  "/read.pdf" } { 
    HTTP::redirect "https://www.main.com/menu" 
    } elseif { [HTTP::uri] starts_with "/Mypersonal/formula" } { 
    HTTP::respond 301 Location "https://www.main.com/colors.html" 
    } else { 
    switch -glob [HTTP::uri] {
    "/page1*" -
    "/page2*" -
  • There is in fact a missing closing brace.

     when HTTP_REQUEST {
         if { [HTTP::uri] contains "/read.pdf" } { 
             HTTP::redirect "https://www.main.com/menu" 
         } elseif { [HTTP::uri] starts_with "/Mypersonal/formula" } { 
             HTTP::respond 301 Location "https://www.main.com/colors.html" 
         } else {  
             switch -glob [HTTP::uri] {
                 "/page1*" -
                 "/page2*" -
                 "/page3*" -
                 "/index*" -
                 "/start*" { 
                     pool orange 
                 }
                 default { 
                     pool green 
                 }
             }
         }
     }