Forum Discussion

Mark_S__182830's avatar
Mark_S__182830
Icon for Nimbostratus rankNimbostratus
Jan 15, 2015

Adding Catchall Behavior to iRule

With the following iRule, when I hit domain.com/ShortName1_Anything here, it will keep the URI portion intact and pass it to the default pool for the VIP.

 

 if { [HTTP::uri] eq "/ShortName1*" } {
    }
elseif { [HTTP::uri] eq "/BLAH/ShortName1*" } {
    HTTP::uri "/ShortName1"
    pool CUSTOM_POOL2
}

My company wants a catch all if the user does not type in a valid /ShortName to redirect to a default one. When I add the following section of code, it states there is a redirect loop in my browser:

 

elseif {([HTTP::host] contains "sub.domain.com") && !([HTTP::uri] eq "/ShortName1*")} {
    HTTP::redirect "/ShortName1"
    pool CUSTOM_POOL1
}

I have tried several different approaches from browsing in the forums, and cannot seem to get a working catch-all.

 

The goal is to have all traffic that is does not resolve to /ShortName1 or /ShortName2, to default to /ShortName1.

 

Am I missing something or perhaps there is an easier way to do this? Thank you.

 

  • I think the issue is with your

    *
    in your if statement. Try this instead...

    switch -glob -- [string tolower [HTTP::uri]] {
        "/shortname1*" {
             Do nothing
        }
        "/blah/shortname*" {
            HTTP::uri "/ShortName1"
            pool CUSTOM_POOL2
        }
        default {
            HTTP::redirect "/ShortName1"
             If you perform the redirect there's no reason to set the pool, because the client will redirect.
        }
    }
    

    Of course, you can make changes like if you need to maintain the original URI after the /shortname or if you want to add more options.

    Hope this helps

  • That worked great - so when you said you think my * was the issue, did you mean here:

     

    && !([HTTP::uri] eq "/ShortName1*")

     

    Thanks a million!

     

    • Michael_Jenkins's avatar
      Michael_Jenkins
      Icon for Cirrostratus rankCirrostratus
      Yea. the "eq" doesn't allow wildcards, so you would have either had to use "string match" or the switch statement (which to me is easier to read than the if statements).
    • Michael_Jenkins's avatar
      Michael_Jenkins
      Icon for Cirrostratus rankCirrostratus
      Glad I could help. It's nice to have a place like DevCentral where we can all get and give answers :)
  • You could have also switched the operator from equals to starts_with, eliminate the * from the matching string, and end the if/elseif with an else statement to set the default action. The switch command is a more efficient way to do the same thing, and uses the * in the URI match to equivocate it to the starts_with operator.

     

  • You could have also switched the operator from equals to starts_with, eliminate the * from the matching string, and end the if/elseif with an else statement to set the default action. The switch command is a more efficient way to do the same thing, and uses the * in the URI match to equivocate it to the starts_with operator.

     

  •  if { [HTTP::uri] starts_with "/ShortName1" } {
         }
    elseif { [HTTP::uri] starts_with "/BLAH/ShortName1" } {
        HTTP::uri "/ShortName1"
        pool CUSTOM_POOL2
    }
    else {([HTTP::host] contains "sub.domain.com") {
        HTTP::redirect "/ShortName1"
    }