Learn F5 Technologies, Get Answers & Share Community Solutions Join DevCentral

Filter by:
  • Solution
  • Technology
Answers

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.

0
Rate this Question

Answers to this Question

placeholder+image
USER ACCEPTED ANSWER & F5 ACCEPTED ANSWER

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}" 
                }
        }
    }
}
0
placeholder+image
USER ACCEPTED ANSWER & F5 ACCEPTED ANSWER

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}" }
        }
    }
}
0
Comments on this Answer
Comment made 20-Jun-2018 by bt 10

Funnily enough we began this deployment about 2 weeks before I posted that. Due to some extremely aggravating technical hurdles we just deployed the new virtual servers on Monday of this week. Contracting third parties to control huge swathes of your environment is great! /s

I would say it hasn't been working the entire time but that's not exactly accurate.

0