Forum Discussion
Vaughn_96017
Nimbostratus
Jun 27, 2007Redirect based on user browser (mobile)
I am writing an iRule that states if the user is coming in via a mobile device to get redirected to a different pool. My question, here is what I have, is this correct?
when HTTP_REQUEST {
if {[HTTP::header User-Agent] contains "Mozilla"}
{pool default-pool} else {pool mobile-pool}
I was givin a list of known mobile user agents to look for but wouldn't it be easier to just look for the non mobile user agents?
Version 9.4 HF4
13 Replies
- Vaughn_96017
Nimbostratus
ok, my bad it should look like this;
when HTTP_REQUEST {
if {[HTTP::header User-Agent] contains "Mozilla"}{
pool default-pool
} else {
pool mobile-pool
}
}
I just need to fix it to accept all non mobile browsers.... - Wil_Schultz_101
Nimbostratus
I use a variation of the followingwhen HTTP_REQUEST { if {([string tolower [HTTP::host]] contains "my.com")} { switch -glob [HTTP::header User-Agent] { "*BlackBerry*" - "*Blackberry*" - "*Java/1.4.1_02*" - "*Blazer*" - "*blazer*" - "*palm*" - "*Palm*" - "*SMARTPHONE*" - "*Smartphone*" - "*smartphone*" - "*Danger*" - "*hiptop*" - "*MOT-*" - "*RAZR*" - "*AUDIOVOX*" - "*Symbian*" - "*symbian*" - "*NOKIA*" - "*Nokia*" - "*Sony Ericsson*" - "*Samsung*" - "*LG 8*" - "*Alcatel 735i*" - "*Nextel*" - "*Windows CE*" - "*NetFront*" { pool mobile_pool } } switch -glob [HTTP::header Accept] { "*text/vnd.wap.wml*" { pool mobile_pool } } } } - Vaughn_96017
Nimbostratus
Thanks wschultz,
Since this is my first irule could you break it down to me like what is -glob, or maybe just blurp what is going on overall? Been working on F5s since 2000, but this is my first taste of iRules.
Thanks so much. - Wil_Schultz_101
Nimbostratus
I can try to give a quick overview but there are folks here that know way more than I 😄
So to get started with iRules you should definitely check out the iRules homepage:
http://devcentral.f5.com/wiki/default.aspx/iRules.HomePage
To give you a quick rundown of this rule in particular, here is the flow:
While the transaction is in the HTTP_REQUEST state, as opposed to HTTP_RESPONSE or some other state. this rule will be run.
The 'if' statement will grab the HTTP::host and make it all lowercase for the comparison, so there is no difference between My.com, MY.com or my.com.
The switch will look at the User-Agent portion of the HTTP::header, you can use several options:
http://devcentral.f5.com/wiki/default.aspx/iRules/switch.html
Glob basically allows the use of some wildcards, so "*BlackBerry*" will match anything that contains "BlackBerry". So the following will look for UserAgents that match, and then use mobile_pool at the end.switch -glob [HTTP::header User-Agent] { "*BlackBerry*" - "*Blackberry*" - "*Java/1.4.1_02*" - "*Blazer*" - "*blazer*" - "*palm*" - "*Palm*" - "*SMARTPHONE*" - "*Smartphone*" - "*smartphone*" - "*Danger*" - "*hiptop*" - "*MOT-*" - "*RAZR*" - "*AUDIOVOX*" - "*Symbian*" - "*symbian*" - "*NOKIA*" - "*Nokia*" - "*Sony Ericsson*" - "*Samsung*" - "*LG 8*" - "*Alcatel 735i*" - "*Nextel*" - "*Windows CE*" - "*NetFront*" { pool mobile_pool } }
If nothing above matches, we look for the Accept portion of the header, if it contains "*text/vnd.wap.wml*" then it will go to the mobile_poolswitch -glob [HTTP::header Accept] { "*text/vnd.wap.wml*" { pool mobile_pool } }
If none of the above matches, it either follows the remaining rules or whatever is set in the profile, which could be a default pool. - JRahm
Admin
iRules use a subset (minus the I/O commands) of standard TCL, so the switch command, while used in irules, is fully documented (along with the other TCL commands supported by iRules) on the TCL reference manual:
http://tmml.sourceforge.net/doc/tcl/switch.html Click here - Vaughn_96017
Nimbostratus
wschultz-
The user agents in your list, does that cover most mobile devices? The reason I am asking is the list they sent me is about 30-40. - Vaughn_96017
Nimbostratus
This is what I was given to redirect as mobile devices;
“*UP.Browser*”
"*(GUI)*"
"*AUDIOVOX*"
"*AU-MIC,*"
"*Alcatel*"
"*BlackBerry*"
"*BENQ*"
"*CASIO*"
"*CDM-*"
"*Ericsson*"
"*EZOS*"
"*kyok*"
"*Kyocera*"
"*LG-*"
"*LGE-*"
"*NEC-*"
"*Nokia*"
"*nok6*"
"*NOKIA*"
"*Mitsu*"
"*MOT-*"
"*mot-*"
"*Motorola*"
"*Sagem*"
"*SAGEM*"
"*Sendo*"
"*SonyEricsson*"
"*Panasonic*"
"*PANTECH*"
"*QC-*"
"*PlayStation Portable*"
"*PHILIPS*"
"*Samsung*"
"*SAMSUNG*"
"*sama*"
"*SEC-S*"
"*SEC-s*"
"*SEC-N*"
"*Sanyo*"
"*SHARP*"
"*Sharp*"
"*SIE-*"
"*Sony*"
"*SymbianOS*"
"*Symbian OS*"
"*portalmmm*"
"*Vodafone/*"
"*KDDI-*"
"*J-PHONE*"
"*Blazer*"
"*AvantGo*"
"*avantbrowser.com*"
"*Danger*"
"*hiptop*"
"*ProxyNet*"
"*IEMobile*"
"*MobileExplorer*"
"*.Web*"
"*Palm*"
"*Series60*"
"*PalmSource*"
"*PalmOS*"
"*MIDP-2.0*"
"*MIDP-1.0*"
"*CLDC-1.0*"
"*CLDC-1.1*"
"*Series60*"
"*Opera Mini*"
"*MobilePhone*"
"*NetFront*"
"*Nitro*"
"*DoCoMo*"
"*Obigo*"
"*PocketPC*"
"*Pocket PC*"
"*RegKing*"
"*Smartphone*"
"*SmartPhone*"
"*EPOC*"
"*Rover*"
"*iPAQ*"
"*Jornada*"
"*iOpus*"
"*iNASSAP*"
"*Minimo*"
"*Plucker*"
"*ERICY*"
"*SoftBank*"
“*DDIPOCKET*"
"*WILLCOM*"
"*YOSPACE*"
"*TagTag*"
"*WinWAP*"
"*UP.Link*"
"*PDXGW*"
"*ASTEL*"
"*WAP1.*"
"*Xiino*"
"*UP/4*"
"*Maemo*"
"*Windows CE*"
"*MSPIE*"
"*Microsoft Pocket Internet Explorer*"
"*Elaine*"
"*EudoraWeb*"
"*ReqwirelessWeb*"
"*jBrowser-WAP*"
"*Lenovo*"
"*M3GATE*"
"*Cellphone*"
"*Sony CMD*"
"*wapsilon*"
"*TELME*"
"*Linux armv*"
"*SONY/COM1*"
"*embedix armv5tel*"
"*Xplore G*"
"*mobileOK DDC*"
"*Google WAP Proxy*"
"*Google CHTML Proxy*"
"*WAP2.*" - Wil_Schultz_101
Nimbostratus
Wow, that's a fairly extensive list 🙂
The list we have seems to work for us, to be honest we actually get complaints because a lot of "Smart Phones" are able to render sites just fine and they don't want to use the mobile page. (See the bypass below)
So creating the iRule would look like the above, here is a version with some of them:switch -glob [HTTP::header User-Agent] { "*BlackBerry*" - "*UP.Browser*" - "*(GUI)*" - "*AUDIOVOX*" - "*AU-MIC,*" - "*Alcatel*" - "*BlackBerry*" - "*BENQ*" { pool mobile_pool } }
Here is the bypass mechanism, which also explains the original if statement:if {!([string tolower [HTTP::host]] == "mobilebypass.my.com")} { switch -glob [HTTP::header User-Agent] { "*BlackBerry*" - "*UP.Browser*" - "*(GUI)*" - "*AUDIOVOX*" - "*AU-MIC,*" - "*Alcatel*" - "*BlackBerry*" - "*BENQ*" { pool mobile_pool } } } - Deb_Allen_18Historic F5 AccountJust answered another post that reminded me of the tcl "return" command.
Instead of setting the flag & conditioning the 2nd switch case on it, you might be able to use "return" @ the end of the first case, with the goal of ending processing for that event immediately, and thus never evaluating the 2nd switch.
However, I've never used "return" inside of switch, so I'm not sure if it would just return you from the switch eval or from the calling event. (I know if you use""return" from inside a loop such as "while" or "foreach" it works as expected, returning from the loop without returning from the calling event.)
Doc for return command is here:
http://tmml.sourceforge.net/doc/tcl/return.html
(Click here)
If you try it, let us know how it worked out for you.
/deb - Deb_Allen_18Historic F5 Account(edited above for clarity)
Help guide the future of your DevCentral Community!
What tools do you use to collaborate? (1min - anonymous)Recent Discussions
Related Content
DevCentral Quicklinks
* 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
Discover DevCentral Connects
