Forum Discussion

dean_02_164511's avatar
dean_02_164511
Icon for Nimbostratus rankNimbostratus
Aug 18, 2015

Geolocation with more than one country

I created the below little snippet to check the country code and redirect. What is the easiest way to format the below if I wanted to see if it was from multiple countries like US, JP, CN, etc.

 

when HTTP_REQUEST {

 

if {[whereis [IP::client_addr]] contains "US"} {

 

HTTP::redirect "http://www.example1.com/"

 

} else {

 

HTTP::redirect "http://www.example2.com/"

 

}

 

}

 

thanks dean

 

1 Reply

  • Using a switch:

    when HTTP_REQUEST {
        switch -glob [whereis [IP::client_addr]] {
            "*US*" -
            "*CA*" -
            "*UK*" {
                HTTP::redirect "http://www.example1.com/"
            }
            
            "*MX*" -
            "*CR*" {
                HTTP::redirect "http://www.example2.com/"
            }
            
            default {
                HTTP::redirect "http://www.example3.com/"
            }
        }
    }
    

    or a datagroup:

    ltm data-group internal country-redirect {
        records {
            CA {
                data http://www.example1.com/
            }
            CR {
                data http://www.example2.com/
            }
            MX {
                data http://www.example2.com/
            }
            UK {
                data http://www.example1.com/
            }
            US {
                data http://www.example1.com/
            }
        }
        type string
    }
    

    along with:

    when HTTP_REQUEST {
        set redir [class match -value -- [whereis [IP::client_addr]] contains country-redirect]
        if { $redir ne "" } {
            HTTP::redirect $redir
        }
        else {
            HTTP::redirect "http://default.example.com/"
        }
    }