For more information regarding the security incident at F5, the actions we are taking to address it, and our ongoing efforts to protect our customers, click here.

Forum Discussion

stephen1's avatar
stephen1
Icon for Nimbostratus rankNimbostratus
Mar 17, 2014

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

4 Replies

  • 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/" 
        }
    }
    
  • 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.

  • 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.

  • InnO's avatar
    InnO
    Icon for Nimbostratus rankNimbostratus

    Hi Ashish,

     

    there may be a typo in "winowsphone", no ?

     

    Thanks, Pascal.