Forum Discussion
Tom_90789
Nimbostratus
Jul 17, 2009Change part of HTTP::host based on User Agent
I am trying to change the HTTP:host for a HTTP::redirect based on the UserAgent identified.
I am able to make it work using a static host, and changing to a different static host, base on the UserAgent, however, we have several mobile web sites, and the logic is if the User Agent detected is in my list, then modify the HTTP::host to m.domain.com but if it is not a mobile device, then send it to www2.domain.com.
request to www.domain.com/someuri/goeshere
if mobile go to m.domain.com/someuri/goesher
if not mobile go to www2.domain.com/someuri/goeshere
This is where I am with it...
when HTTP_REQUEST {
set host [HTTP::host]
set ua [string tolower [HTTP::header "User-Agent"]]
set hostman [split $host "."]
if { [matchclass $ua contains $::useragent_list] } {
set hostman [lreplace $hostman 0 0 m]
[join $hostman "."]
{HTTP::redirect] "http://$hostman[HTTP::uri]"}
else {
set hostman [lreplace $hostman 0 0 "www2"]
[join $hostman "."]
[HTTP::redirect] "http://$hostman[HTTP::uri]"}
}
}
- The_Bhattman
Nimbostratus
I am assuming that useragent_list contains the list of mobile user-agents.when HTTP_REQUEST { if { ([HTTP::host] equals "www.domain.com" ) and ([HTTP:uri] equals "someuri/goeshere") } { if { [matchclass [HTTP::header "User-Agent"] contains $::useragent_list ] } { HTTP::redirect "http://m.mobile.com[HTTP::uri]" } else { HTTP::redirect "http://www2.domain.com[HTTP::uri]" } } }
- Tom_90789
Nimbostratus
yes that would be simpler.. however, my goal is to make this rule work for any site whose URLs are constructed properly without having to change the rule. I probably didnt make that clear enough. - The_Bhattman
Nimbostratus
Understood. You can do another type of command that might work nicely for you called "string map" (Click here)when HTTP_REQUEST { if { [matchclass [string to lower [HTTP::header "User-Agent"]] contains $::useragent_list ] } { HTTP::redirect "http://[string map -nocase {"www." "m." } [HTTP::host]][HTTP::uri]" } else { HTTP::redirect "http://[string map -nocase {"www." "www2." } [HTTP::host]][HTTP::uri]" } }
- Tom_90789
Nimbostratus
Very nice.. thanks.. just learning some of this TCL, but have people breathing down my neck now and needed a quick answer. Appreciate it - The_Bhattman
Nimbostratus
Tell them to take tic-tac and relax ;-) - ingard
Nimbostratus
I was wondering, how would it be easiest to maintain the useragent lists? It doest say here where the list is maintained. Is it included from a textfile located on the bigip itself? - ingard
Nimbostratus
I was wondering, how would it be easiest to maintain the useragent lists? It doest say here where the list is maintained. Is it included from a textfile located on the bigip itself? - ingard
Nimbostratus
I was wondering, how would it be easiest to maintain the useragent lists? It doest say here where the list is maintained. Is it included from a textfile located on the bigip itself? - The_Bhattman
Nimbostratus
It would be contained in a class object that you defined within the LTM's GUI - The_Bhattman
Nimbostratus
It would be contained in a class object that you defined within the LTM's GUI
Recent Discussions
Related Content
DevCentral Quicklinks
* Getting Started on DevCentral
* Community Guidelines
* Community Terms of Use / EULA
* Community Ranking Explained
* Community Resources
* Contact the DevCentral Team
* Update MFA on account.f5.com
Discover DevCentral Connects