Forum Discussion

bt_308832's avatar
bt_308832
Icon for Nimbostratus rankNimbostratus
Jun 20, 2018

Detect Geo Location With iRule and Redirect

Hello,

I have this iRule:

when HTTP_REQUEST {
    if { [string tolower [HTTP::host]] contains "example.com" && [HTTP::path] eq "/" }{
        set country [whereis [IP::client_addr] country]
        switch $country {
            "US" -
            "UK" -
            "CA" - { HTTP::redirect "https://example.com/${country}" }
        }
    }
}

The goal being to detect US, UK and CA users and then redirect them to a region specific page on the site. However with the rule enabled we just get back empty reply from F5. Turning it off or requesting example.com// (multiple trailing slashes) works. I'm thinking it has something to do with how the if statement is structure since adding additional slash characters corrects the problem but this is a bit out of my normal wheelhouse. Usually the F5 tasks I get delegated are updating SSL certificates and simple redirects.

  • Anesh's avatar
    Anesh
    Icon for Cirrostratus rankCirrostratus

    try

    when HTTP_REQUEST {
    
        if { [string tolower [HTTP::host]] equals "www.example.com" && [HTTP::path] eq "/" }{
    
            set client_ip [IP::client_addr]
            set country [string tolower [whereis $client_ip country]]
    
         switch $country {
                "us" -
                "uk" -
                "ca"  { 
                    HTTP::redirect "https://example.com/${country}" 
                    }
            }
        }
    }
    
  • Your code is from this link where Leonardo answered.

     

    Does it mean this code never worked since september?

     

    If the redirect is on the same server but to a subdirectory, redirect to a relative URL instead of absolute URL

     

    when HTTP_REQUEST {
        if { [string tolower [HTTP::host]] contains "example.com" && [HTTP::path] eq "/" }{
            set country [whereis [IP::client_addr] country]
            switch $country {
                "US" -
                "UK" -
                "CA" - { HTTP::redirect "/${country}" }
            }
        }
    }