Forum Discussion
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
- Philippe_CLOUP
Employee
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 ?
- Philippe_CLOUP
Employee
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_30338Historic 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"))}
- htuytela_37346
Nimbostratus
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
- Kevin_Stewart
Employee
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 } } }
- htuytela_37346
Nimbostratus
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.
Recent Discussions
Related Content
* Getting Started on DevCentral
* Community Guidelines
* Community Terms of Use / EULA
* Community Ranking Explained
* Community Resources
* Contact the DevCentral Team
* Update MFA on account.f5.com