Wednesday, February 19, 2014

Parse specific query parameter value

URL: www.mydomain.com?query1=value1&query2=value2

iRule
when HTTP_REQUEST {
 log local0.  [URI::query "?[HTTP::query]" query1]
}

Output
value1

Mobile iRule - Honor user preference to view desktop site

Use case:
If the user is accessing from mobile, redirect to mobile website.
When the user choose the option to view desktop site from mobile webpage, redirect to desktop site. Keep the user in desktop site for 30 mins and put them back on mobile website.

Solution:
The solution uses cookie expiry logic.
When the user clicks "view desktop site" from mobile webpage, a query parameter (Ex: "allowMobile=y") is passed for tracking.

1) Please refer to the post mobile detection iRule on this blog. This rule is extension based on previous blog item.

2) Update or create an iRule named "mobile-rule-v2"

when HTTP_REQUEST {
 set useragent [string tolower [HTTP::header User-Agent]]
 set mobilehost "m.mydomain.com"
 set uri [HTTP::uri]
 set query [string tolower [HTTP::query]]
 set ismobile false

 if { [matchclass $useragent contains mobile-user-agents ] } {
  set ismobile true
 }
 elseif { $useragent contains "android" } {
  if { $useragent contains "mobile" } {
   set ismobile true
  }
 }

 if { $ismobile } {
   set hascookie false
   if { $query contains "allowMobile=y" } {
     if { not ( [HTTP::cookie exists "my-mobile-cookie"] ) } {
HTTP::cookie insert name "my-mobile-cookie" value "none" path "/"
HTTP::cookie expires "my-mobile-cookie" 1800
set hascookie true
     }
   }

   if { not ( $hascookie ) } {
    # Do the redirect logic here
   }
 }
}

# This response block is important
when HTTP_RESPONSE {
    if { [info exists "my-mobile-cookie"] } {
unset "my-mobile-cookie"
HTTP::cookie insert name "my-mobile-cookie" value "none" path "/"
HTTP::cookie expires "my-mobile-cookie" 1800
    }
}

Mobile Detection iRule

Use case:
Redirect www.mydomain.com (desktop website) to m.mydomain.com (mobile website)

1) Create a data group
Local Traffic > iRules > Data Group List > Create
Name: mobile-user-agents
Type: String

Add Records
Example:
String iphone
Value: (leave this empty)

Here is the list of mobile user agents I used.

avantgo
bada
bb
blackberry
blazer
bolt
compal
elaine
fennec
gobrowser
hiptop
iemobile
iphone
iris
kindle
lge
maemo
mib
midp
minimo
mmp
netfront
palm
phone
semc-browser
skyfire
symbian
teashark
teleca
uzardvodafone
wap

2) Create an iRule
Local Traffic > iRules > iRules List > Create
Name: mobile-rule-v1
Definition:

when HTTP_REQUEST {
 set useragent [string tolower [HTTP::header User-Agent]]
 set mobilehost "m.mydomain.com"
 set uri [HTTP::uri]
 set ismobile false

 if { [matchclass $useragent contains mobile-user-agents ] } {
  set ismobile true
 }
 elseif { $useragent contains "android" } {
  if { $useragent contains "mobile" } {
   set ismobile true
  }
 }

 if { $ismobile } {
   HTTP::redirect "http://$mobilehost$uri"
   # Do any other logic here
 }
}

3) Add the iRule to the virtual server

Newer›  ‹Older