Forum Discussion

dundovic_25174's avatar
dundovic_25174
Icon for Nimbostratus rankNimbostratus
Oct 25, 2011

'if' gets ignored

I wrote following iRule in attempt to redirect two specific requests to desired pages and would like to have any other request with path beginning with '/1' to get redirected to homepage:

 

 

---

 

 

when HTTP_REQUEST {

 

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

 

set host [string tolower [HTTP::host]]

 

set link "$host$uri"

 

switch -exact $link {

 

"www.domain.com/1/2/something" {HTTP::redirect "http://www.domain.com/something.html"}

 

"www.domain.com/1/2/something2" {HTTP::redirect "http://www.domain.com/something2.html"}

 

if { [HTTP::host] equals "www.domain.com" and [HTTP::path] starts_with "/1" } then {

 

HTTP::redirect "http://www.domain.com"

 

}

 

}

 

}

 

 

---

 

 

The explicit redirects work fine, but the 'if' statement that checks for '/1' in path start gets totally ignored. Could somebody clarify why?

 

 

Regards,

 

 

D.

 

 

 

  • If is not a valid conditional test within a switch statement. If you want to add one, you'll have to do it in the "default" case.

    Something like this should work:

    when HTTP_REQUEST {
      set uri [string tolower [HTTP::uri]]
      set host [string tolower [HTTP::host]]
      set link "$host$uri"
      switch -exact $link {
        "www.domain.com/1/2/something" {
          HTTP::redirect "http://www.domain.com/something.html"
        }
        "www.domain.com/1/2/something2" {
          HTTP::redirect "http://www.domain.com/something2.html"
        }
        default {
          if { [HTTP::host] equals "www.domain.com" and [HTTP::path] starts_with "/1" } {
            HTTP::redirect "http://www.domain.com"
          }
        }
    }

    Another way to do this would be without the if by using a wildcard comparison (-glob) in the switch like this:

    when HTTP_REQUEST {
      set uri [string tolower [HTTP::uri]]
      set host [string tolower [HTTP::host]]
      set link "$host$uri"
      switch -glob $link {
        "www.domain.com/1/2/something" {
          HTTP::redirect "http://www.domain.com/something.html"
        }
        "www.domain.com/1/2/something2" {
          HTTP::redirect "http://www.domain.com/something2.html"
        }
        "www.domain.com/1*" {
          HTTP::redirect "http://www.domain.com"
        }
    }

    Hope this helps...

  • The 'switch -glob' method is absolutely perfect and works like a charm. Also seems the way it's written takes less CPU processing. Thanks, again!

     

  • Glad to hear it! If you haven't seen this yet, check out the "iRules 101" series of articles we've written. Lots of good stuff in there on the basics of the various commands: http://devcentral.f5.com/wiki/iRules.iRules101.ashx