Forum Discussion

muzammil_88686's avatar
muzammil_88686
Icon for Nimbostratus rankNimbostratus
Nov 22, 2012

Detecting Mobile Devices and Redirecting to different URL

Dear Dev Team,

 

-> Curretly we have the below iRule.

 

==========================================================

 

when HTTP_REQUEST {

 

 

switch [string tolower [HTTP::host]] {

 

"www.test.com" -

 

"test.com" -

 

"10.1.1.1" {

 

 

 

Check and redirect request

 

switch [string tolower [HTTP::path]] {

 

 

"/" -

 

"/abc/123" {HTTP::redirect "http://www.test.com/abc/123/mypage/?reset=true"}

 

}

 

}

 

}

 

}

 

==========================================================

 

 

New requirement is, apart from the above, if any mobile device(need to be detected by iRule) who is trying to access the same "www.test.com" or "www.test.com/abc/123" URL need to be redirected to "http://www.test.com/abc/123/mypage/mobile-page"

 

 

Could you pls help me on this?

 

 

Thank you in advance!

 

 

10 Replies

  • Create a Data Group with the relevant mobile User Agent strings call 'user-agent-dg' and then update your iRule as follows;

    
    when HTTP_REQUEST {
     switch [string tolower [HTTP::host]] {
      "www.test.com" -
      "test.com" -
      "10.1.1.1" {
        Check and redirect request
       switch [string tolower [HTTP::path]] {
       "/" -
       "/abc/123" { 
        if { [class match [string tolower [HTTP::header User-Agent]] contains user-agent-dg ] } {
         HTTP::redirect "http://www.test.com/abc/123/mypage/mobile-page" }
        else {
         HTTP::redirect "http://www.test.com/abc/123/mypage/?reset=true" }
        }
       }
      }
     }
    }
    
  • Dear Steve,

     

     

    Thank you for your quick response!

     

     

    Could you help me how can I create a DG for different mobile devices? Is there any other post or article I can refer?
  • In the GUI go to: Local Traffic > iRules > Data Group List > Create > Type: String and enter each user agent string you want to check for in the string field, leave the value field blank.
  • Thank you Steve!

     

     

    How can I log if the Mobile device is detected? I tried using the log as below in If statement but it is not working.

     

     

    if { [class match [string tolower [HTTP::header User-Agent]] contains user-agent-dg ] } {

     

    HTTP::redirect "http://www.test.com/abc/123/mypage/mobile-page"

     

    log local0. "[IP::client_addr]:[TCP::client_port]: UA: [HTTP::header "User-Agent"], redirecting"

     

    }

     

    else {

     

    HTTP::redirect "http://www.test.com/abc/123/mypage/?reset=true" }

     

    }
  • Can you clarify not working please? Also, remove the quotation marks "" from "User-Agent".
  • Dear Steve,

     

     

    I have one more question...

     

     

    I have created a Datagroup using GUI with the below strings.

     

     

    android

     

    blackberry

     

    palm

     

    sonyericsson

     

    nokia

     

    samsung

     

    symbian

     

     

    Want to know the below things.

     

     

    1) I have defined the above strings using small letters. Is this strings are case sensitive? I mean, since I have defined "android", if any request is coming with "Android" then still it will work?

     

     

    2) Also I have noted that when I add strings "sie" and "windows" PC users(not mobile users) then the URL is redirecting to Mobile page instead of normal page

     

     

    3) Could you help me getting the most popular Mobile devices strings which I can add in Datagroup?
  • Hi muzammil,

     

     

    Here's a related post with some suggestions:

     

    https://devcentral.f5.com/community/group/aft/86313/asg/5086381

     

     

    Aaron
  • It is very old post but I have got similar requirement.

     

    Is there a possibility to detect if a mobile app installed in iPhone or Android, and if the particular app installed redirect it to a link1 otherwise redirect to a different link2. Pseudocode is something like this;

     

    check if the mobile device has facebook(for example) True: 1 redirect to false: 2 redirect to

     

    Is this possible to implement in F5 using iRules or some otherway?

     

    TIA, John