Forum Discussion
Damien_Turner_1
Nimbostratus
Aug 02, 2010Redirect if your a mobile user....
Hi all, I was wondering if I could have some help!
I have been asked if it is possible to use an iRule to redirect a mobile user to a new URL.
An example would be....
user on an iPhone goes to www.mysite.com but as they are a mobile user the URL gets changed to m.mysite.com and the correct formatted site is then shown.
I've looked through quite a few similar topics but none seem to redirect the URL.
I have a a pool of web servers but they serve all traffic, but I guess I could create anther pool.
I'm still quite new to iRules but I know they rule!
Thank You
Damien
11 Replies
- Chris_Miller
Altostratus
Damien,
The first thing you need to do is implement the logic for your rule to detect whether the user is an iPhone. For this, you'll use the HTTP::header command to query the "User-Agent" header.
http://devcentral.f5.com/wiki/default.aspx/iRules/HTTP__header.html
You'll do an iRule like this and put it on your www.mysite.com VIPwhen HTTP_REQUEST { if { [HTTP::header "User-Agent"] contains "iPhone"] } { HTTP::redirect "http://m.mysite.com" } }
When you want to do more than just iPhones, you'll want to create a "datagroup" which contains all types of phones and reference that instead of just the string "iPhone"
Shall I assume the VIP to which m.mysite.com points has a pool of web servers setup to render mobile-specific content? - Damien_Turner_1
Nimbostratus
Hi Chris thank you for replying.
Your right the VIP point to the same servers but will display a different site as the incoming domain will be different.
If I want to use a datagroup would it look like this....when HTTP_REQUEST { if { [HTTP::header "User-Agent" ] equals $::MobileUserAgents } { HTTP::redirect "http://mobile.holidayextras.co.uk" } Uncomment the line below to turn on logging. log local0. " Mobile Redirected" } The datagroup then has string *iPhone* *iPad* etc My iRule editor said it has passed the checker which is always a good sign! Regards Damien - hoolio
Cirrostratus
If you change equals to contains that should work. If you're on 9.4.4 or higher, you should remove the $:: prefix from the reference to the datagroup name in the iRule. You might also want to preserve the originally requested URI in the redirect using 'HTTP::redirect "http://mobile.example.co.uk[HTTP::uri]".
Aaron - Damien_Turner_1
Nimbostratus
Hi Aaron thank you for helping me again, I have made the changes so my rule now looks like this....when HTTP_REQUEST { if { [HTTP::header "User-Agent" ] contains $::MobileUserAgents } { HTTP::redirect "http://mobile.holidayextras.co.uk[HTTP::uri]" log local0. "iPhone Found" } Uncomment the line below to turn on logging. log local0. "Mobile Redirected: [HTTP::header "User-Agent"] -Phone Redirected" }code] My DataGroup is a string with.... *iPhone* *iPad* *iPod* I like My Apple stuff! But the iRule dosn't seem to be working as nothing is being written to the log, if I turn on the global log I can see iPhone user agents passing through.... Aug 4 09:33:17 tmm tmm[1800]: Rule Mobile_UserAgent_Redirect_iRule : Mobile Redirected: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729) -Phone Redirected Aug 4 09:33:17 tmm tmm[1800]: Rule Mobile_UserAgent_Redirect_iRule : Mobile Redirected: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729) -Phone Redirected Aug 4 09:33:17 tmm tmm[1800]: Rule Mobile_UserAgent_Redirect_iRule : Mobile Redirected: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729) -Phone Redirected Aug 4 09:33:17 tmm tmm[1800]: Rule Mobile_UserAgent_Redirect_iRule : Mobile Redirected: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729) -Phone Redirected Aug 4 09:33:17 tmm tmm[1800]: Rule Mobile_UserAgent_Redirect_iRule : Mobile Redirected: Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_0_1 like Mac OS X; en-us) AppleWebKit/532.9 (KHTML, like Gecko) Version/4.0.5 Mobile/8A306 Safari/6531.22.7 -Phone Redirected Aug 4 09:33:17 tmm tmm[1800]: Rule Mobile_UserAgent_Redirect_iRule : Mobile Redirected: Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_0_1 like Mac OS X; en-us) AppleWebKit/532.9 (KHTML, like Gecko) Version/4.0.5 Mobile/8A306 Safari/6531.22.7 -Phone Redirected Aug 4 09:33:17 tmm tmm[1800]: Rule Mobile_UserAgent_Redirect_iRule : Mobile Redirected: Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_0_1 like Mac OS X; en-us) AppleWebKit/532.9 (KHTML, like Gecko) Version/4.0.5 Mobile/8A306 Safari/6531.22.7 -Phone Redirected Aug 4 09:33:18 tmm tmm[1800]: Rule Mobile_UserAgent_Redirect_iRule : Mobile Redirected: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729) -Phone Redirected I'm confused! Also you said if I'm running 9.4.4 then I should remove the $:: from the datagroup, I have 9.4.8 HF4 running but it wont let me take out the $::, when I try and pass the rule through the irule editor it keeps telling me my irule is invalid. Thank you for your help guys! - hoolio
Cirrostratus
Hi Damien,
Sorry I missed it before. Can you use matchclass to check the datagroup?
if { [matchclass [HTTP::header "User-Agent" ] contains $::MobileUserAgents] } {
Aaron - Damien_Turner_1
Nimbostratus
Hi Aaron, I missed that one too!
Now looks like this...when HTTP_REQUEST { if {[matchclass [HTTP::header "User-Agent" ] contains $::MobileUserAgents] } { HTTP::redirect "http://m.holidayextras.co.uk[HTTP::uri]" log local0. "iPhone Found" } Uncomment the line below to turn on logging. log local0. "Mobile Redirected: [HTTP::header "User-Agent"] -Phone Redirected" } But its not picking up my iphone, could I have the information in the dataclass wrong? - hoolio
Cirrostratus
Can you remove the * from the datagroup entries? Those will be interpreted literally--not as wildcards.
Are you on 9.4.4 or higher? If so, can you also remove the $:: prefix from MobileUserAgents?
Else, can you add logging to the rule and check /var/log/ltm for the output:when HTTP_REQUEST { log local0. "[IP::client_addr]:[TCP::client_port]: UA: [HTTP::header "User-Agent"]" if {[matchclass [HTTP::header "User-Agent"] contains $::MobileUserAgents] } { HTTP::redirect "http://m.holidayextras.co.uk[HTTP::uri]" log local0. "[IP::client_addr]:[TCP::client_port]: UA: [HTTP::header "User-Agent"], redirecting" } }
Aaron - Damien_Turner_1
Nimbostratus
Hi Aaron, taking the * out of the datagroup fixed it, the iRule now see's my iPhone and redirects.
I have tried to take out the $:: but my LTM keeps saying the iRUle is invalid if I do so, I'm on version 9.4.8 HF4, when I put $:: the iRule passes without a problem. - pmaubo2_55685
Nimbostratus
Hi,
I am doing somewhat the same, the only thing different is if I have an iphone come in I want to direct to one site and if it's an Android then I want it to direct to a different site. - Nicolas_Pla_681
Nimbostratus
Hi, i have a list of almost all mobile devices in this irule. hope that you find it usefull
if {[HTTP::header User-Agent] matches_regex "(Android|iP(hone|od)|Symbian|BlackBerry|Opera M(ini|obi)|IEMobile|GoBrowser|Blazer|BOLT|Fennec|Iris|Maemo|MIB|Minimo|NetFront|SEMC-Browser|Skyfire|TeaShark|Teleca|uZard)"}{
HTTP::respond 301 Location "http://m.your.site.com"}
}
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
