Forum Discussion

rdessert_76127's avatar
rdessert_76127
Icon for Nimbostratus rankNimbostratus
Jul 19, 2012

Help with adding mobile redirect to existing irule

Hi all,

 

 

I have been asked to redirect a handful of mobile device types (user agents) to our mobile homepage and am having some trouble getting the irule configured properly.

 

 

My current homepage irule is as follows:

 

 

when HTTP_REQUEST {

 

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

 

HTTP::respond 301 Location "http://www.site.com[HTTP::uri]"

 

return

 

} else {

 

log local0.alert "BEGIN Host:[HTTP::host]-[HTTP::uri]-[HTTP::path]"

 

switch -glob [string tolower [HTTP::uri]] {

 

"/" -

 

"/*" -

 

"/\\?*" -

 

"/foo/*" -

 

"/news/*" -

 

"/sites/*" -

 

"/node/*"

 

{

 

log local0.alert "op_pool Host:[HTTP::host]-[HTTP::uri]"

 

pool op_pool

 

return

 

}

 

default {

 

pool def_pool

 

}

 

}

 

}

 

}

 

 

 

I need to redirect the following user agents to http://m.site.com

 

 

blackberry

 

wp7

 

iphone

 

androi

 

 

Any suggestions on how / where to add the mobile redirect. I'm thinking it needs to go at the beginning but don't know the best syntax.

 

 

Thanks for any help!!

 

 

Rich

 

 

 

 

  • e.g.

    [root@ve10:Active] config  b virtual bar list
    virtual bar {
       snat automap
       pool foo
       destination 172.28.19.79:80
       ip protocol 6
       rules myrule
       profiles {
          http {}
          tcp {}
       }
    }
    [root@ve10:Active] config  b rule myrule list
    rule myrule {
       when HTTP_REQUEST {
       if {[HTTP::host] equals "site.com"} {
          if {[class match -- [string tolower [HTTP::header value "User-Agent"]] equals mobile_class]} {
             HTTP::respond 301 Location "http://m.site.com[HTTP::uri]"
          } else {
             HTTP::respond 301 Location "http://www.site.com[HTTP::uri]"
          }
       } else {
          switch -glob [string tolower [HTTP::uri]] {
             "/" -
             "/*" -
             "/\\?*" -
             "/foo/*" -
             "/news/*" -
             "/sites/*" -
             "/node/*" { pool op_pool }
             default { pool def_pool }
          }
       }
    }
    }
    
    [root@ve10:Active] config  curl -I http://172.28.19.79/something -H "Host: site.com"
    HTTP/1.0 301 Moved Permanently
    Location: http://www.site.com/something
    Server: BigIP
    Connection: Keep-Alive
    Content-Length: 0
    
    [root@ve10:Active] config  curl -I http://172.28.19.79/something -H "Host: site.com" -H "User-Agent: iphone"
    HTTP/1.0 301 Moved Permanently
    Location: http://m.site.com/something
    Server: BigIP
    Connection: Keep-Alive
    Content-Length: 0
    
    
  • When I tested this with a browser it wasn't working although it was working by running the curl command from the box itself. I ended up having to change this line as follow...

     

     

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

     

     

    to

     

     

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

     

     

    Had me scratching my head for a bit, but it finally clicked :-)

     

     

    Rich