Forum Discussion
iRule Geo Targeting
Some minor cosmetic changes:
when HTTP_REQUEST {
if { [string tolower [HTTP::uri]] starts_with "/geo" } {
Parse the client IP from X-Forwarded For as Client IP is proxied.
if { not ( [HTTP::header exists X-Forwarded-For] ) } {
set state [class match -value [whereis [IP::client_addr] state] equals uk_regions]
switch $state {
"Leeds" {
HTTP::redirect "http://62.190.1.1/ab/leeds"
}
"York" {
HTTP::redirect "http://62.190.1.1/ab/york"
}
"grlond" {
HTTP::redirect "http://www.google.com/news"
}
default {
pool DEV-HTTP-POOL
}
}
} else {
what do we do if the X-Forwarded-For header is missing?
}
} else {
what do we do if the URI doesn't start with /geo?
}
}
A couple of key points also:
-
In the previous rule you had the "state" switch outside the X-Forwarded-For header/class match conditional, which meant the $state variable could have not existed when the switch statement was performed - causing an error. I moved all of that into the X-Forwarded-For header conditional.
-
The default pool action will only be triggered if the URI starts with "/geo", the X-Forwarded-For header doesn't exist, and class match fails. You should probably have else conditions outside of these to account for:
- What happens if the X-Forwarded-For exists
- What happens if the URI deosn't start with "/geo"
Recent Discussions
Related Content
* Getting Started on DevCentral
* Community Guidelines
* Community Terms of Use / EULA
* Community Ranking Explained
* Community Resources
* Contact the DevCentral Team
* Update MFA on account.f5.com