Forum Discussion

OCC_68606's avatar
OCC_68606
Icon for Nimbostratus rankNimbostratus
Jun 14, 2011

iRule Redirect Mobile Site

I have this iRule to identify mobile browser, for example apple devices:

 

 

 

 

 

when HTTP_REQUEST {

 

if { [string tolower [HTTP::host]] contains "www.mainsite.com" } {

 

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

 

"*iphone*" -

 

"*ipod*" -

 

"*ipad*" {

 

HTTP::redirect "http://mobilesite.com[HTTP::uri]"

 

return

 

}

 

}

 

}

 

}

 

 

 

mobilesite.com has a link to www.mainsite.com (In case you don´t like be in mobile site)

 

 

 

I want to do something that manage if you are in mobile site and you want to go to main site the iRule does not enter in a loop.

 

 

 

Any ideas??

 

 

 

Jorge.

 

  • Check your other post here for more info as well:

     

    https://devcentral.f5.com/community/group/aft/2161149/asg/502274875

     

     

    Aaron
  • if you want only one page, is this usable?

    [root@ve10:Active] config  b virtual bar list
    virtual bar {
       snat automap
       pool foo
       destination 172.28.19.252:443
       ip protocol 6
       rules myrule
       profiles {
          clientssl {
             clientside
          }
          http {}
          tcp {}
       }
    }
    [root@ve10:Active] config  b rule myrule list
    rule myrule {
       when HTTP_REQUEST {
       if { [HTTP::host] equals "home.example.co.in" } {
          if { [URI::query "?&[HTTP::query]" main] equals "true" } {
             return
          }
    
          if { [string tolower [HTTP::header "User-Agent"]] contains "iphone" } {
             HTTP::redirect "https://lite.example.co.in"
          }
    
       } elseif { [URI::query "?&[HTTP::query]" main] equals "true" } {
          HTTP::redirect "https://home.example.co.in?main=true"
       }
    }
    }
    
    [root@ve10:Active] config  curl -Ik https://home.example.co.in/something -H "User-Agent: iphone5"
    HTTP/1.0 302 Found
    Location: https://lite.example.co.in
    Server: BigIP
    Connection: Keep-Alive
    Content-Length: 0
    
    [root@ve10:Active] config  curl -Ik https://lite.example.co.in/something?main=true -H "User-Agent: iphone5"
    HTTP/1.0 302 Found
    Location: https://home.example.co.in?main=true
    Server: BigIP
    Connection: Keep-Alive
    Content-Length: 0
    
    [root@ve10:Active] config  curl -Ik https://home.example.co.in?main=true -H "User-Agent: iphone5"
    HTTP/1.1 200 OK
    Date: Wed, 06 Mar 2013 11:27:51 GMT
    Server: Apache/2.2.3 (CentOS)
    Last-Modified: Sat, 27 Oct 2012 03:22:35 GMT
    ETag: "4183f3-59-f28f94c0"
    Accept-Ranges: bytes
    Content-Length: 89
    Content-Type: text/html; charset=UTF-8
    
    
  • Dear Nitass,

     

    Thanks so much for your help, if i want to use mre mobile types will below iRule works

     

    when HTTP_REQUEST {

     

    if { [HTTP::host] equals "home.example.co.in" } {

     

    if { [URI::query "?&[HTTP::query]" main] equals "true" } {

     

    return

     

    }

     

    if { [string tolower [HTTP::header "User-Agent"]] contains "iphone" -

     

    "android" -

     

    "ipad" -

     

    "blackberry" -

     

    "windowsphone" -

     

    "windows phone" -

     

    "windowsce" -

     

    "windows ce" -

     

    "symbainos" -

     

    "symbain os" -

     

    "symbain" -

     

    "java" } {

     

    HTTP::redirect "https://lite.example.co.in"

     

    }

     

    } elseif { [URI::query "?&[HTTP::query]" main] equals "true" } {

     

    HTTP::redirect "https://home.example.co.in?main=true"

     

    }

     

    }

     

    Waiting for your reply.

     

    Regards,

     

    Ashish Takawale.

     

  • if i want to use mre mobile types will below iRule worksno, syntax is not correct. you should use "switch" or "class match" with data group.

    this is an example of class match with data group.

    [root@ve10:Active] config  b class mobile_class list
    class mobile_class {
       {
          "android"
          "iphone"
       }
    }
    [root@ve10:Active] config  b rule myrule list
    rule myrule {
       when HTTP_REQUEST {
       if { [HTTP::host] equals "home.example.co.in" } {
          if { [URI::query "?&[HTTP::query]" main] equals "true" } {
             return
          }
    
          if { [class match -- [string tolower [HTTP::header "User-Agent"]] contains mobile_class] } {
             HTTP::redirect "https://lite.example.co.in"
          }
    
       } elseif { [URI::query "?&[HTTP::query]" main] equals "true" } {
          HTTP::redirect "https://home.example.co.in?main=true"
       }
    }
    }
    
  • 2) Now in web page of mobile site (https://mobile.example.co.in) user have one link IF YOU WANT TO GO MAIN SITE PLEASE CLICK HERE (http://home.example.co.in) one query string will be passed for the same i.e ismain==true and user have to redirect to http://home.example.co.inaccording to your irule logic, won't user get redirected to mobile site again? i assume client would create a new connection to bigip.
  • so the above iRule which i have mentioned on above will work fine?? or else do i need some changes in it.

     

     

  • just now i have tried the iRule given by you in my UAT environment, but havent work, when i assign this iRule to Virtual Server of home.example.co.in, the main page for home.example.co.in is not opiningwas there any error in /var/log/ltm?