Forum Discussion

boczon_108037's avatar
boczon_108037
Icon for Nimbostratus rankNimbostratus
Aug 16, 2011

Where is and Country

Any one have any experience using whereis and redirecting based on country?. I have an irule I pieced together from examples found on devcentral and can get the rule to work for US States part way but it completely fails when using country.

 

 

The goal is to add the rule to a VIP that will redirect to country specific folders on the same VIP. once a user hits the country specific folder they will get a cookie to identify the country choice so I do not want the rule to run very time but can't figure out how to get past the initial looping when a user does not have the country cookie.

 

 

 

The second part is the rule does not seem to work when I use country. Using the rule below I get get the redirect if I change $country to $state but no luck with Country

 

 

 

 

 

when CLIENT_ACCEPTED {

 

set country [class match -value [whereis [IP::client_addr]] equals CTS_COUNTRY]

 

set state [class match -value [whereis [IP::client_addr] abbrev] equals US_STATE]

 

log local0. "The country is [whereis [IP::client_addr] country] and the state is [whereis [IP::client_addr] abbrev]"

 

}

 

when HTTP_REQUEST {

 

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

 

log local0. "Cookie found [HTTP::cookie SelectedCountryCode]"

 

event HTTP_REQUEST disable }

 

elseif {($country ne "")}{

 

switch $country {

 

PA {HTTP::redirect http://www.testdomain.com/nsurvey}

 

US {HTTP::redirect http://www.testdomain.com/nsurvey}

 

CA {HTTP::redirect http://www.google.com}

 

AU {HTTP::redirect http://www.ctstestdomain.com/nsurvey}

 

GB {HTTP::redirect http://www.testdomain.com/nsurvey}

 

default {pool www.testdomain.com}

 

}

 

}

 

}

 

 

 

 

 

The logging does show Country and state but no redirect for country

 

 

: The country is US and the state is PA

 

: The country is GB and the state is 17

 

 

 

If I do away with the HTTP::cookie exists and use this I can get around the looping issue but the developers would like to use cookie

 

 

 

if { [class match [HTTP::uri] contains US_STATE]} {

 

event HTTP_REQUEST disable }

 

 

 

 

Any suggestions?

 

 

 

 

 

  • I would suggest looking at the Heat Maps Tech Tip that Colin posted.

     

     

    The continents post was in the second post of the series (http://devcentral.f5.com/Tutorials/TechTips/tabid/63/articleType/ArticleView/articleId/1086353/Heatmaps-iRules-Style-Part2.aspx), but the entire series is worth reading.

     

     

    The Wiki Entry for whereis could be useful too: http://devcentral.f5.com/wiki/iRules.whereis.ashx

     

     

    whereis country

     

    Returns a string containing the two-letter country code (JP)
  • Thanks I'll take a look at the Heatmaps and I was able to valdate that the country code was being returned.
  • Colin_Walker_12's avatar
    Colin_Walker_12
    Historic F5 Account
    Here's the specific line Michael is referencing:

    
    set cloc [whereis [IP::client_addr] country] 
    

    Thanks for the mention. 😉

    Colin
  •  

    Thanks for the info but forgive me if I am missing the obvious. I was not able to determine where i was making a mistake in my rule. I am unsure why the rule would work for state but not country.

     

     

  • when HTTP_REQUEST {
      set debug 1
      if {[HTTP::cookie exists "SelectedCountryCode"]} {
        set country [HTTP::cookie "SelectedCountryCode"]
        if {$debug} { log local0. "SelectedCountryCode = [HTTP::cookie SelectedCountryCode ]" }
      }  else  {
        set country [whereis [IP::client_addr] country]
      }
      if {$debug} {  log local0. "Country is $country" }
      switch $country {
          PA { set redirect http://www.testdomain.com/nsurvey}
          US { set redirect http://www.testdomain.com/nsurvey}
          CA { set redirect http://www.google.com}
          AU { set redirect http://www.ctstestdomain.com/nsurvey}
          GB { set redirect http://www.testdomain.com/nsurvey}
          default { 
            if {$debug} { log local0. "Unmatched country was $country" }
            set redirect http://www.testdomain.com
         }
      }
      if {$debug} {  log local0. "Redirected to $redirect" }
      HTTP::redirect $redirect
    }

     

    I didn't see any data classes listed so took them out for this code. Test and check the logging to validate the results. Then you can add the classes back in. Is the last pool command in your code meant to be a redirect? The pool name looks like a website name. You can turn off logging by setting debug 0 on the second line.

     

     

    Kevin (Jarvil)
  • Thanks i'll give it a try the last pool command is just sending to a default pool. The name of the pool just happens to be the FQDN of the web site it is supporting.
  • If its the default pool attached to the virtual then you don't need the pool command. It will go to that pool unless you redefine it somewhere.

     

     

    Kevin (Jarvil)

     

  • By the way, I know this is an old thread, but I was looking for something related to whereis and came across this. I think the problem was that the first set command is missing the "country":

     

    set country [class match -value [whereis [IP::client_addr]] equals CTS_COUNTRY]

     

     

    Change that to:

     

    set country [class match -value [whereis [IP::client_addr] country] equals CTS_COUNTRY]

     

     

    And it may work as anticipated. The log command uses the "country" which is why that works, but the $country variable simply wasn't being set properly. Had the log command used the variable $country instead of doing another lookup, then the original poster would have seen the default "whereis" returning a TCL list of {continent country region city} since no modifier was used.

     

     

    Since nobody else mentioned that specifically, I thought I'd reply to a stale thread.