Forum Discussion

htuytela_37346's avatar
htuytela_37346
Icon for Nimbostratus rankNimbostratus
Dec 12, 2013

iRule check for user-agent and set cookie

Hi,

I'm trying to get an iRule to work that checks on User-Agent, being IE6,7,8 and setting a cookie. Basically, I want those browsers to be redirected to another page, and set a cookie. When the user returns, he should not be redirected anymore. That is why I'm setting this cookie. This is what I have, although I'm getting "line 1: [wrong args] [when HTTP_REQUEST { " here. All help is appreciated. A side question, how can I set the expiry time for that cookie ?

` when HTTP_REQUEST {
if {([string tolower [HTTP::header User-Agent]] contains "MSIE6" or "MSIE7" or "MSIE8"}) and (not [HTTP::cookie] exists "test")}{
HTTP::redirect "https://test-lab.foo.com/CG510_APR_RedirecttestURL/"
HTTP::cookie insert "test"

else pool site-10.130.14.98_29710 }`

Thanks,

Hans

6 Replies

  • Hi htuyela, you syntax in the if looks wrong (sorry, i have not bigip close to me to try). the curly brace is closed before the second part of the condition in the if and you cannot test multiple options in the same condition, and the square bracket need to be closed for the string tolower function:

     

    if {([string tolower [HTTP::header User-Agent]] contains "MSIE6" or "MSIE7" or "MSIE8"}) and (not [HTTP::cookie] exists "test")}

     

    it should be like this

     

    if { ([string tolower [HTTP::header User-Agent]] contains "MSIE6"]) and (not [HTTP::cookie] exists "test")}

     

    if you want to test multiple options for the brworser, then i would recommend you to test the existence of the cookie first, and then iterate with a "switch" command, the browser types.

     

    Does that make sense ?

     

  • sorry, i was too quick. The "square bracket was ok" ;-)

     

    it should be like this:

     

    if { ([string tolower [HTTP::header User-Agent]] contains "MSIE6") and (not ([HTTP::cookie] exists "test"))}

     

  • Christian_30338's avatar
    Christian_30338
    Historic F5 Account

    Also if you are using "string tolower" i think the comparison string should be in lowercase as well. something like this.

    if {([string tolower [HTTP::header User-Agent]] contains "msie6") and (not ([HTTP::cookie] exists "test"))}
    
  • thank you for the reply, unfortunately, this did not help. I'm progressing slowly, as the suggestion on using the switch syntax made me try this :

    when HTTP_REQUEST {
     switch -glob [HTTP::header value "User-Agent"] {
        "*MSIE 6*" -
        "*MSIE 7*" -
        "*MSIE 8*" {
     switch -glob [HTTP::cookie exists "test"] ne ""
        HTTP::redirect "http://test-lab.foo.com:9610/CG510_APR_RedirectCapriURL/"} 
          default {
          pool site-10.130.14.98_29710
        }
      }
    }
    

    This rule is accepted and there is a difference in testing using IE & Mozilla, so the browser selection seems to work. I enabled cooking persistence on the VS with a cookie named test. However, that did not help. To me, it looks like the iRule is not able to read the cookie.

    regards,

    Hans

  • Try this:

    when HTTP_REQUEST {
        switch -glob [HTTP::header value "User-Agent"] {
            "*MSIE 6*" -
            "*MSIE 7*" -
            "*MSIE 8*" {
                if { not ( [HTTP::cookie exists "test"] ) } {
                    HTTP::redirect "http://test-lab.foo.com:9610/CG510_APR_RedirectCapriURL/"
                } else {
                     what do you do if the cookie exists?
                }
            default {
                pool site-10.130.14.98_29710
            }
        }
    }
    
  • Thanks Kevin, unfortunately, that rule also gives me errors. Meanwhile, I decided to drop the requiemenent on the cooke, and the browser selection alone works fine, so I'm good now.

     

    thank you for your help on this.