Forum Discussion

cokeefe28_17139's avatar
cokeefe28_17139
Icon for Nimbostratus rankNimbostratus
Jan 16, 2012

Mobile Device Detection with logic

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 (for simplicity, we will say only iPhone, iPad, Android, Blackberry, and Windows Mobile)

 

 

 

2. Redirect the user to a mobile optimized site, but:

 

a. If the user has already been to the mobile site, then exit from detection and allow to full site.

 

b. If the user is going to a specific URI on the full site, redirect to the specific site on the mobile (trying to accomplish with a Class Search

 

c. All others should go to mobile site if detected, else go to full site.

 

d. I will also need some form of tracking (a) to ensure the user does not go back to the mobile site for the duration of the session (not sure how to do this other than cookie which is not supported by ALL devices.

 

 

 

Below is what I have so far, but it does not seem to be working, and I do not know why???

 

 

 

when HTTP_REQUEST {

 

set uri [string trimright [HTTP::uri] "/" ]

 

set mobile_site "http://m.site.com"

 

set full_site "http://www.site.com"

 

 

if {$uri eq "/?mobile=false"} {

 

HTTP::redirect $full_site

 

event disable

 

} else {

 

set mobile_opt_uri [class search -value mobile_phone_optimized eq [string tolower $uri]]

 

switch -glob [string tolower [HTTP::header User-Agent]] {

 

"*blackberry*" -

 

"*wp7*" -

 

"*androi*" -

 

"*iphone*" -

 

"*ipad*" {

 

if {$mobile_opt_uri ne "" } {

 

HTTP::redirect $mobile_site }

 

 

HTTP::redirect $mobile_site/$mobile_opt_uri

 

}

 

}

 

}

 

 

 

if { [string tolower [HTTP::header Accept]] contains "vnd.wap.wml" } {

 

HTTP::redirect $mobile_site

 

return

 

}

 

}

 

 

7 Replies

  • is www.site.com and m.site.com same virtual server or different?

     

     

    not sure if i understand correctly or not. this is what i think. the m_rule is assigned to m.site.com virtual server and the www_rule is assigned to www.site.com virtual server.

     

     

    the www2.site.com host is used if user wants to get content from www.site.com even he is using mobile device.

     

     

    [root@ve1023:Active] config b rule m_rule list

     

    rule m_rule {

     

    when HTTP_REQUEST {

     

    if {not [class match -- [string tolower [HTTP::header "User-Agent"]] contains mobile_class]}{

     

    HTTP::redirect "http://www.site.com/"

     

    }

     

    }

     

    }

     

     

    [root@ve1023:Active] config b rule www_rule list

     

    rule www_rule {

     

    when HTTP_REQUEST {

     

    if {[HTTP::host] equals "www2.site.com"} {

     

    return

     

    }

     

    if {[class match -- [string tolower [HTTP::header "User-Agent"]] contains mobile_class]}{

     

    if {[class match -- [string tolower [HTTP::uri]] contains specific_uri_class]} {

     

    set mobile_opt_uri [class match -value [string tolower [HTTP::uri]] contains specific_uri_class]

     

    HTTP::redirect ""

     

    } else {

     

    HTTP::redirect "http://m.site.com/"

     

    }

     

    }

     

    }

     

    }

     

     

  • First off, thank you for your response. To further clarify, here is an example....

     

     

    If I go to www.site.com from my mobile device, I want to detect the mobile device and redirect to m.site.com. If I go to a specific URL as defined in a class (easier to expand the list as devlopers create additional optimized pages for mobile devices) for example www.site.com/uri1 and the device is detected as a mobile device, then redirect to m.site.com/uri1. If the class search comes up empty, redirect to m.site.com.

     

     

     

    If I have gone through the detection already (as a user) and I choose to go to the full site by clicking a hyperlink on the mobile site (www.site.com/?mobile=false) then redirect back to www.site.com and exit from further processing. Of course I need a way to track the user so that they do not continue in a loop of going back to the mobile site.

     

     

     

    I hope this helps! I thought that I was half way there but I just can't seem to make the logic after switch work. It always redirects me back to the mobile site. I was thinking that this would only need to go onto the www.site.com VS.....and yes, they are two separate VS's but I have not emotional tie to it being that way.

     

  • I choose to go to the full site by clicking a hyperlink on the mobile site (www.site.com/?mobile=false) then redirect back to www.site.com and exit from further processing.i think the problem is here since after redirection, browser may create a brand new connection to www virtual server and it will be redirected to mobile virtual server again. local variable is not shared across connection. for global variable, i am not sure what data we can use. ip address might not be a good idea because mobile device may be behind nat device. cookie is not an option as you mentioned it is not supported on all device.

     

     

    the rule above i used different FQDN in url (e.g. www2.site.com/?mobile=false), so we will be able to detect it later.
  • How does everyone else do this? Is it typically done in code on the server or detected in apache, iis, etc.?
  • You should be able to set a cookie to track whether the client has opted for either the full site or mobile site based on requests to www.site.com/?mobile=false or a URI which selects the mobile site. Here's a rough idea of what I'm thinking:

    
     Main (non-mobile) site iRule
    when HTTP_REQUEST {
    
     Use full site if user has main (non-mobile) site cookie
    if {[HTTP::cookie site] eq "main"}{
    
     Do not redirect clients who have manually opted for the main site
    } else {
    
     User has not manually opted for the main site
     Check if the client is a mobile
    if {[class match -- [string tolower [HTTP::header "User-Agent"]] contains mobile_class]}{
    if {[class match -- [string tolower [HTTP::uri]] contains specific_uri_class]} {
    set mobile_opt_uri [class match -value [string tolower [HTTP::uri]] contains specific_uri_class]
    HTTP::redirect "http://m.site.com$mobile_opt_uri"
    } else {
    HTTP::redirect "http://m.site.com/"
    }
    }
    }
    }
    
     Mobile site iRule
    when HTTP_REQUEST {
    
     Check if request is to a URI which indicates a mobile user wanting to use the main site
    if {[HTTP::query] eq "mobile=false"}{
    
     Redirect to main site with a cookie to track the user wants to stay on the main site
    HTTP::respond 302 Location "http://www.site.com/" Set-Cookie "Site=main; domain=.site.com"; path="/";"
    }
    }
    

    Aaron
  • I have gotten the following to work....a little of Aaron and a little of mine, but I think that Nitass is right....I may not be able to track this with a cookie since most mobile devices don't use cookies. Src IP will not work because there are the cases of NAT. I think that the only way that I can get this to work is by only executing this on the front login page (say index.htm) and have all the other pages reference home.htm. That way I can put something in there that says If not HTTP::uri /index.htm....exit. Thoughts? Here is the iRule.

     

     

    when HTTP_REQUEST {

     

    Set a varible to the URI minus the trailing slash (i.e. /TEST not /TEST/)

     

    set uri [string trimright [HTTP::uri] "/" ]

     

    set mobile_site "http://m.site.com"

     

    if {[HTTP::cookie site] eq "main"}{

     

    } else {

     

     

    Case Select if you are a using a mobile phone (Blackberry, Windows Mobile, Android, iPhone, iPad)

     

    switch -glob [string tolower [HTTP::header User-Agent]] {

     

    "*blackberry*" -

     

    "*wp7*" -

     

    "*androi*" -

     

    "*iphone*" -

     

    "*ipad*" {

     

    if {[class match -- [string tolower $uri] contains mobile_phone_optimized_uri]}{

     

    set mobile_opt_uri [class search -value mobile_phone_optimized_uri eq [string tolower $uri]]

     

    HTTP::redirect $mobile_site/$mobile_opt_uri

     

    event disable

     

    } else {

     

    HTTP::redirect $mobile_site

     

    }

     

    }

     

    default {

     

    }

     

    }

     

    }

     

    Are you using WAP?

     

    if { [string tolower [HTTP::header Accept]] contains "vnd.wap.wml" } {

     

    HTTP::redirect "http://m.site.com"

     

    return

     

    }

     

     

    if { [HTTP::header exists "MSISDN"] } {

     

    HTTP::redirect "http://m.site.com[HTTP::uri]"

     

    return

     

    }

     

    }