Forum Discussion
Detect mobile device use iRule
dear All,
I seem to be spinning my wheels here and I am not sure why. I want to do the following:
1.Detect if a user is coming from a mobile device
Redirect the user to a mobile optimized site, but: 1.if user used mobile device enter www.mydomain.com will be redirect m.mydomain.com , my iRule example:
when HTTP_REQUEST {
if { [HTTP::header User-Agent] matches_regex "(Android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|Android|iP(hone|od|ad)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up.(browser|link)|vodafone|wap|windows (ce|phone)|xda|xiino"} { HTTP::redirect "http://m.mydomain.com" } }
2.if mobile device user click change site icon , the icon url is http://www.mydomain.com?fr=mb , mboile device user will be redirect http://wwww.mndomain.com web site, how can determine in irule
please help me
- IheartF5_45022Nacreous
Hi you need to use {} to enclose your regex instead of "". Also for the iRule try this;-
if { [HTTP::header User-Agent] matches_regex {(Android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|Android|iP(hone|od|ad)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up.(browser|link)|vodafone|wap|windows (ce|phone)|xda|xiino}} { It's a mobile device if {[HTTP::uri] eq "/?fr=mb"} { HTTP::redirect "http://www.mydomain.com/" } else { HTTP::redirect "http://m.mydomain.com/" } }
- IheartF5_45022Nacreous
Hi you just need to add some loggin to work out why it's not matching as you expect. I've just show a fragment below to show where to log;-
if {[HTTP::uri] eq "/?fr=mb"} { log local0. "Redirecting [HTTP::uri] to http://www.settour.com.tw/" HTTP::redirect "http://www.settour.com.tw/" } else { log local0. "Redirecting [HTTP::uri] to http://m.settour.com.tw/" HTTP::redirect "http://m.settour.com.tw/" }
Then you have to check the logs in /var/log/ltm - you should quickly figure out where you are going wrong.
- Ashish_Ram_Tak1Nimbostratus
Hello Stephen,
You can try below iRule, I have same kind of requirement and the below iRule is working fine.
when HTTP_REQUEST { set uri [string tolower [HTTP::uri] ]
set mobile_site "http://m.mydomain.com"
set full_site "http://www.mydomain.com"
switch -glob [string tolower [HTTP::header User-Agent]] { "*iphone*" - "*android*" - "*windows phone*" - "*windows ce*" - "*bada*" - "*bb10*" - "*blackberry*" - "*symbinos*" - "*symbain os*" - "*symbian*" - "*java*" - "*winowsphone*" - "*windowsce*" { HTTP::redirect "http://m.mydomain.com" return } } }
Regards, Ashish.
- InnONimbostratus
Hi Ashish,
there may be a typo in "winowsphone", no ?
Thanks, Pascal.
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