Forum Discussion

Clayton_Wagner_'s avatar
Clayton_Wagner_
Icon for Nimbostratus rankNimbostratus
Jan 17, 2013

Base Domain Redirect with user-agent detection

I have the rule below in place and it works, but I have to create a seperate irule for every domain I host. I'm trying to create a single rule that I can use on all of my digital properties. The requirement is all requests from mobile devices will redirect to the m. version of the site. I can do this with string replace, but that doesn't work for a "base domain" request.

 

hostnames to replace:

 

domain.com

 

www.domain.com

 

shop.domain.com

 

Here is the 2 rules I have:

 

The problem with this rule is that i have to specify the "domain.com" in the redirect, so I have to create a separate irule for every domain.

 

when HTTP_REQUEST {

 

if {[HTTP::cookie exists "MobileOptOut"]}{

 

return

 

}

 

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

 

"*iphone*" -

 

"*mobile*" -

 

"*ipod*" -

 

"*blackberry*" -

 

"*opera\ mobi*" -

 

"*htc*" -

 

"*silk*" -

 

"*googlebotmobile*" -

 

"*msn\ mobile\ proxy*" -

 

"*android*" {

 

HTTP::redirect http://m.domain.com[HTTP::uri]

 

return

 

}

 

}

 

}

 

The problem with this rule is that it will not redirect the base domain "domain.com"

 

when HTTP_REQUEST {

 

if {[HTTP::cookie exists "MobileOptOut"]}{

 

return

 

}

 

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

 

"*iphone*" -

 

"*mobile*" -

 

"*ipod*" -

 

"*blackberry*" -

 

"*opera\ mobi*" -

 

"*htc*" -

 

"*silk*" -

 

"*googlebotmobile*" -

 

"*msn\ mobile\ proxy*" -

 

"*android*" {

 

HTTP::redirect "http://[string map -nocase {"www." "m." } [HTTP::host]][HTTP::uri]"

 

return

 

}

 

}

 

}

 

Does anyone have any ideas how I can strip out the base domain from the request and redirect to m.domain.com?

 

 

TIA - Clayton

 

 

2 Replies

  • is this useful?

    [root@ve10:Active] config  b rule myrule list
    rule myrule {
       when RULE_INIT {
      set domain_list { domain.com www.domain.com shop.domain.com }
      foreach domain $domain_list {
        set last [string last . $domain end]
        set sec [string last . $domain [expr {$last-1}]]
        log local0. "domain: $domain | base domain: [string range $domain [expr {$sec+1}] end]"
      }
    }
    }
    
    [root@ve10:Active] config  tail -f /var/log/ltm
    Jan 17 15:54:51 local/ve10 err mcpd[3776]: 01020066:3: The requested rule (myrule) already exists in partition Common.
    Jan 17 15:54:51 local/tmm info tmm[4884]: Rule myrule : domain: domain.com | base domain: domain.com
    Jan 17 15:54:51 local/tmm info tmm[4884]: Rule myrule : domain: www.domain.com | base domain: domain.com
    Jan 17 15:54:51 local/tmm info tmm[4884]: Rule myrule : domain: shop.domain.com | base domain: domain.com