Forum Discussion

pvrobinsontx2_2's avatar
pvrobinsontx2_2
Icon for Nimbostratus rankNimbostratus
Nov 12, 2015

iRule redirect based on uri or user-agent match logic.

Hello, looking for some direction here. I am trying create a rule which will match a specific set of uri and redirect if true to a prefered site and quit processing the irule. If the uri doesn't match, it will continue to the next match test for mobile in the header user-agent. If a match is found the redirect is to an other site.

Problem is, I can match the uri, but if it doesn't match the uri it doesn't seem to continue nor match when the user-agent is mobile.

Can someone tell me what I'm doing wrong here. I hope the formatting looks correct. Thanks.... ///pvr

when HTTP_REQUEST { switch -glob [string tolower [HTTP::uri]] {

        "/about_news" -
        "/*.pdf" {
           HTTP::redirect "https://www.mysite.com[HTTP::uri]" }
        }

switch -glob [string tolower [HTTP::header User-Agent]] {

        "*mobile*" {
           HTTP::redirect "https://www.myothersite.com[HTTP::uri]"  
        }
     }

}

4 Replies

  • BinaryCanary_19's avatar
    BinaryCanary_19
    Historic F5 Account

    To the naked eye, i don't see anything wrong with your logic.

     

    You might want to add logging statements to track the values you're matching for, and log every block execution reaches to make sure there is not something unexpected happening.

     

    • pvrobinsontx2_2's avatar
      pvrobinsontx2_2
      Icon for Nimbostratus rankNimbostratus
      Thanks... That was my thinking that the logic was right. I've placed log local2. statements , the values are there but when it comes to the mobile section there is simply no execution as best I can tell. It's almost like there is a bug somewhere in running multiple switch statements in the same iRule. If the 1st uri match doesn't catch, I get an instant connection reset.
    • BinaryCanary_19's avatar
      BinaryCanary_19
      Historic F5 Account
      Stanislas's example above does not use multiple switch statements. give that a go.
  • Hi,

    the irule may work when :

    • URI doesn't match and user agent doesn't match
    • URI match and user agent doesn't match
    • URI doesn't match and user agent match

    But the irule may cause a tcl error when both URI and user agent match

    add a

    return
    command after each
    HTTP::redirect
    to leave irule.

    use

    HTTP::path
    instead of
    HTTP::uri
    in switch...
    HTTP::uri
    may contain query string which can be dynamic.

    when HTTP_REQUEST { 
        switch -glob [string tolower [HTTP::path]] {
            "/about_news" -
            "*.pdf" {
                HTTP::redirect "https://www.mysite.com[HTTP::uri]" 
                return
            }
            default {
                if {[string tolower [HTTP::header User-Agent]] contains "mobile"} {
                    HTTP::redirect "https://www.myothersite.com[HTTP::uri]"  
                    return
                }
            }
        }
    }