Region HTTP redirect

Problem this snippet solves:

The following is an example iRule where you can redirect the same websites across different regions based on the clients IP address.

$::regionXclients is a datagroup that contains network addresses of clients in the region1 which can be any region you wish to base your logic on. $::pool_regionXservers corresponds with the resource of there the pool of servers

For example: region1clients contain clients in the east coast, region2clients contain clients in the west coast, region3clients contain clients in the Europe and finally region4clients contain clients for asia pacific. pool_region2servers contain servers for region1clients, pool_region2server contain servers for region2clients, etc. However, if say pool_region1servers is down then it will send region1clients over to server to pool_region2servers and so forth.

Code :

if { ([matchclass [IP::client_addr] eq $::region1clients]) {  
      if { [active_members pool_region1servers] > 0 } {
          HTTP::redirect "http://[HTTP::host]/region1"
      } else if { [active_members  pool_region2servers] > 0 } {
          HTTP::redirect "http://[HTTP::host]/region2"
      } else if { [active_members pool_region3servers] > 0 } {
          HTTP::redirect "http://[HTTP::host]/region3"
      } else if { [active_members pool_region4servers] > 0 } {
          HTTP::redirect "http://[HTTP::host]/region4"
          }  
   } else if { ([matchclass [IP::client_addr] eq $::region2clients]) {
      if { [active_members pool_region2servers] > 0 } {
          HTTP::redirect "http://[HTTP::host]/region2"
      } else if { [active_members pool_region3servers] > 0 } {
          HTTP::redirect "http://[HTTP::host]/region3"
      } else if { [active_members pool_region4servers] > 0 } {
          HTTP::redirect "http://[HTTP::host]/region4"
      } else if { [active_members pool_region1servers] > 0 } {
          HTTP::redirect "http://[HTTP::host]/region1"
          }
   } else if { ([matchclass [IP::client_addr] eq $::region3clients]) {
      if { [active_members pool_region3servers] > 0 } {
          HTTP::redirect "http://[HTTP::host]/region3"
      } else if { [active_members pool_region4servers] > 0 } {
          HTTP::redirect "http://[HTTP::host]/region4"
      } else if { [active_members pool_region1servers] > 0 } {
          HTTP::redirect "http://[HTTP::host]/region1"
      } else if { [active_members pool_region2servers] > 0 } {
          HTTP::redirect "http://[HTTP::host]/region2"
          }
   } else if { ([matchclass [IP::client_addr] eq $::region4clients]) {
      if { [active_members pool_region4servers] > 0 } {
          HTTP::redirect "http://[HTTP::host]/region4"
      } else if { [active_members pool_region1servers] > 0 } {
          HTTP::redirect "http://[HTTP::host]/region1"
      } else if { [active_members pool_region2servers] > 0 } {
          HTTP::redirect "http://[HTTP::host]/region2"
      } else if { [active_members pool_region3servers] > 0 } {
          HTTP::redirect "http://[HTTP::host]/region3"
          } 
    }
}
Published Mar 18, 2015
Version 1.0

Was this article helpful?

No CommentsBe the first to comment