Forum Discussion

RoyalM_205794's avatar
RoyalM_205794
Icon for Nimbostratus rankNimbostratus
Dec 14, 2015

GeoIP iRule redirects with switch

Hi All,

I'm trying to create an 'all-in-one' iRule to handle a few things at once. Firstly I need to allow a few whitelists through (CDN, office addresses etc) then based on the GeoIP location redirect to either a pool or a different domain.

I have the iRule working in a sense but for a reason unknown to me, redirecting to pools doesn't work - and may be related to how I've laid out the iRule.

I have to include the switch because some of the Virtual Servers this applies to will need 10+ redirect entries to different websites, as does the elseif statements displaying content from the relevant pools. Although, if this works in the switch statement that will be easier to manage but I've tried to include pools in the switch statement and again, no joy.

Basically, if it's not CDN and it's not in the whitelist > Redirect to a new URL or an active Pool based on the client IP.

when HTTP_REQUEST {
set countries [whereis [IP::client_addr] country]

if {[class match [string tolower [HTTP::header value "Via"]] contains CDN_HEADERS] } {
return

} elseif {[class match [IP::client_addr] equals Whitelist]} {
return

} elseif {($countries equals "NL")} { pool NL-splash

} elseif {($countries equals "RU")} { pool RU-splash

} else {

switch [whereis [IP::client_addr] country] {

"UK" { HTTP::respond 301 noserver Location "https://www.google.com" }
"US" { HTTP::respond 301 noserver Location "https://www.google.com" }

            default {
         return

            }
         }
       }
    }

Thanks

  • Hi RoyalM,

     

    I would structure and format the code in this way...

     

    when HTTP_REQUEST {
        if {[class match [string tolower [HTTP::header value "Via"]] contains CDN_HEADERS] } then {
            return
        } elseif {[class match [IP::client_addr] equals Whitelist] } then {
            return
        } else {
            switch -exact -- [whereis [IP::client_addr] country] "NL" { 
                pool NL-splash
            } "RU" {
                pool RU-splash
            } "UK" {
                HTTP::respond 301 noserver Location "https://www.google.com" 
            } "US" { 
                HTTP::respond 301 noserver Location "https://www.google.com"
            } default {
                return
            }
        }
    }

    Give it a try, maybe its already resolving your issues?

     

    Cheers, Kai

     

  • Hi Kai,

     

    That's a much better way of laying out the rule and appears to have done the trick :)

     

    Thanks for your help!

     

    RoyalM

     

    • Kai_Wilke's avatar
      Kai_Wilke
      Icon for MVP rankMVP
      You're welcome, glad to see its working out for you... ;-)