Redirect_to_URLs_based_on_a_class_file_containing_network_and_desired_site_information

Problem this snippet solves:

This iRule was developed to do HTTP redirects to a particular hostname (defined in a class) for scenarios where you have knowledge of particular source networks that clients are coming from and desire to have these clients directed to a specific hostname.

The virtual server where this iRule lives will typically be accessed following a BIG-IP GTM and ideally, the target hostname is also a BIG-IP GTM Wide-IP where Global Availability is used to deal with the possibility that the sites you are listing in the class file are unavailable.

The advantage of doing this redirection (as opposed to simply letting BIG-IP GTM resolve Wide-IP's using its topology feature) is that rather than GTM where only the Client/Local DNS is visible to GTM; with an iRule running on an LTM virtual server, the actual client IP address is visible. This allows very accurate Source Network based traffic management for scenarios where this is practical.

Code :

class network_to_site_map {
   {
      host 10.37.3.210 { "site5" }
      host 10.37.6.107 { "site4" }
      network 192.168.1.0/24 { "site1" }
      network 192.168.2.0/24 { "site2" }
      host 10.8.5.1 { "site3" }
   }
}
rule source_ipnet_redirector {
   when HTTP_REQUEST {
     set redirecttarget [class match -value [IP::client_addr] equals network_to_site_map]
     if { $redirecttarget ne "")} {
        HTTP::redirect "http://$redirecttarget.domain.com[HTTP::uri]"
     } else {
        # HTTP::redirect "http://defaultsite.domain.com[HTTP::uri]"
        HTTP::respond 404 content "You're attempting access from an invalid IP address"
     } 
   }
}
Published Mar 18, 2015
Version 1.0
No CommentsBe the first to comment