Forum Discussion
reldar_76465
Nimbostratus
Jul 13, 2011redirect mobile devices
Hi,
I'm looking for a script that will enable us to redirect all traffic coming from mobile devices to a mobile site.
Many thanks for the help.
Roy
11 Replies
- hoolio
Cirrostratus
Hi Roy,
Here's a recent post to start with:
http://devcentral.f5.com/Community/GroupDetails/tabid/1082223/asg/50/aft/1178780/showtab/groupforums/Default.aspx
Aaron - reldar_76465
Nimbostratus
Many thanks for your reply.
now I created the below iRule to redirect all mobile traffic coming to my site to another site.
the thing is that on that 'other site', i want to have the reversed iRule that would redirect back to the original site all traffic that is NOT a mobile.
I would appreciate some guidance as how to 'reverse' the below script by using 'not'. something like 'if any of the below does not exist - redirect to www....'
thanks
when HTTP_REQUEST {
switch -glob [string tolower [HTTP::header User-Agent]] {
"*blackberry*" -
"*ipad*" - {
HTTP::redirect "http://m.mysite.com/[HTTP::uri]"
return
}
}
} - Michael_Yates
Nimbostratus
Hi reldar,
As you can see in the link that Hoolio posted, there are quite a few User-Agents (and more are being added with each release of a new Web-Mobile capable device), so you are going to have to maintain a list one way or the other.
The list of non-mobile browser User-Agents (which is probably far more manageable) or a list of mobile User-Agents in order to fully tackle what you are trying.
I would suggest changing your iRule on the mobile Virtual Server to capture the non-mobile browser agents and redirect them to your non-mobile site and on your non-mobile site reverse the logic and say if you are not a non-mobile browser redirect to the mobile site. Then you should only have to keep track of one set of User-Agents.
You could also put a default action to log any unknown User-Agents when you want to see what the site is actually processing (and comment it out when not needed):when HTTP_REQUEST { switch -glob [string tolower [HTTP::header User-Agent]] { "*blackberry*" - "*ipad*" - { log local0. "Device Access Attempt [HTTP::header User-Agent]" HTTP::redirect "http://m.mysite.com/[HTTP::uri]" return } } }
(It is also worth noting that if people find out how you are filtering their traffic, it is possible to change the User-Agent of most devices to get around this type of filter). - Colin_Walker_12Historic F5 AccountI tend to agree with Michael about it being a chore and a half to maintain these lists. That being said, here's the logic you were asking for, which is a reverse of what you already have:
when HTTP_REQUEST { switch -glob [string tolower [HTTP::header User-Agent]] { "*blackberry*" - "*ipad*" { } default { HTTP::redirect "http://mysite.com/[HTTP::uri]" } } }
Basically all you need to do is make the cascading list of fall-through logic end with something other than the redirect. It could be sending the request to a pool or simply doing nothing, as above. Then everything that doesn't match one of those cases, which are the mobile cases, will fall through to the redirect and get sent to your non mobile site.
Colin - reldar_76465
Nimbostratus
Many thanks again for your replies.
All you mentioned is very true, but doing the opposite (create a script to 'allow' desktops, rather than redirect mobile) is more risky. I can live with the fact that mobile will go to the original site, but I can't afford desktops being redirected to the mobile site. Anyway, if you keep the main players in the list, there is 80% coverage which is good enough for me.
one question though - I tried Colin's suggestion and it didn't work for me. Did I miss anything? Do you need to add something to it?
Thanks again - Michael_Yates
Nimbostratus
There was no process handling for the event in Colin's post and there was a rogue "-" in mine.
This one will redirect for blackberry or ipad, and log the User-Agent any other devices.when HTTP_REQUEST { switch -glob [string tolower [HTTP::header User-Agent]] { "*blackberry*" - "*ipad*" { HTTP::redirect "http://m.mysite.com/[HTTP::uri]" return } default { log local0. "Device Access Attempt [HTTP::header User-Agent]" } } } - hoolio
Cirrostratus
One tiny suggestion:
You can remove the / in the redirect between the hostname and [HTTP::uri]:when HTTP_REQUEST { switch -glob [string tolower [HTTP::header User-Agent]] { "*blackberry*" - "*ipad*" { HTTP::redirect "http://m.mysite.com[HTTP::uri]" } default { log local0. "Device Access Attempt [HTTP::header User-Agent]" } } }
Also, you don't need the return in the switch statement as only one switch case will ever get executed.
Aaron - Mario_Eury_6049
Nimbostratus
Mike,
I like your suggestion of using a list of non-mobile browsers and the User-Agents. How would I accomplish that? What I change the agents you have to simply "*Mozilla*"- and "*IE*"- etc.... - Michael_Yates
Nimbostratus
Hi Mario Eury,
There are a few additional examples and a partial list in this post: (http://devcentral.f5.com/Default.aspx?tabid=53&forumid=5&tpage=1&view=topic&postid=15116)
The best way to get a list is to create a logging statement (log local0. "Device Access Attempt [HTTP::header User-Agent]").
This will put the User-Agent into your /var/log/ltm log and you can grep it with the iRule name to see what is being logged.
You could also go here, but this is quite a list: http://www.user-agents.org/
Keep in mind that if you use a datagroup list to split between Desktop Browser Traffic and Mobile Traffic, that either way you are going to have to maintain a list of one or the other and every time a new mobile device comes out you may need to update your list.
The entries you listed contain wildcards so that they will catch all Mozilla based browsers “*Mozilla*”, or all versions of Internet Explorer “*IE*”, but I would be careful using them since I know that there are mobile versions of IE, and perhaps Mozilla based browsers. - Mario_Eury_6049
Nimbostratus
Michael,
My web team would like to know if there is a way to redirect mobile devices. But when users have a mobile device and prefer the full site, have a link on our mobile site that takes them to the full page. I've tried this and it loops. Apple iPad would be an example.
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
