Forum Discussion
nrelihan_68143
Nimbostratus
Jul 08, 2011Redirecting to regional website depending on user DNS.
Hey all, I'm quite new to F5 irules first of all.
Perhaps I'll tell you what I have working first and then explain to you what additional things I'd like to do.
What I have working
When somebody goes to www.fakewebsite.com it looks at their dns location and redirects it to their a regional website, in my example here, its Ireland and UK. So when somebody goes to this website when in Ireland it will redirect them to www.fakewebsite.com/ireland and to www.fakewebsite.co.uk when in the UK.
I have two pools setup, one for ireland and one for the UK.
Global traffic irule
when DNS_REQUEST {
if {[whereis [IP::client_addr]] contains "IE"} {
pool pool-Ireland
} elseif {[whereis [IP::client_addr]] contains "GB"} {
pool pool-UK
} else {
pool pool-default
pool-UK Local traffic irule
when HTTP_REQUEST {
if {[HTTP::uri] equals "/" } {
HTTP::respond 302 Location "http://www.fakewebsite.com/ireland/"
}
}
pool-Ireland Local traffic irule
when HTTP_REQUEST {
if {[HTTP::uri] equals "/"} {
HTTP::respond 302 Location "http://www.fakewebsite.co.uk/"
}
}
What Im looking to do
Im looking to enhance this like so:
For example when somebody is located in the UK types www.fakewebsite.com/pictures the irule would redirect them to the UK website but including the static page they requested, www.fakewebsite.co.uk/pictures.
When somebody is located in irealnd they type www.fakewebsite.com/pictures the irule would redirect them to www.fakewebsite.com/ireland/pictures.
What I'm afriad though is that if somebody based in Ireland but for some reason had a UK ip address and visa versa, typed in www.fakewebsite.com/ireland, it would still re-direct them to the .co.uk website. So in other words they would never be able to get to the .com/ireland website.
So would it be possible to include a rule that would not allow this to occur.
Maybe the rule might say, if they type in www.fakewebsite.com/ireland, do not put it though an irule?
Any help is appreciated, my brain is melting trying to get working code for this!
Thanks alot!
Neil
19 Replies
- Peter_72728
Nimbostratus
I don't think the logic to add URI rewriting that retains the requested URI (i.e. www.fakewebsite.CO.UK/pictures) would be too hard. In fact you might get away with something like this in your pool-UK Local traffic irule:
if { not [HTTP::uri] starts_with "/ireland" } {
HTTP::respond 302 Location http://www.fakewebsite.com/ireland/[HTTP::uri]
}
In response to your last question...
Unless your rule did something else (maybe add a cookie?), I'm not sure how the iRule would be able to differentiate a request that was redirected to .com/ireland verses one where the user typed that in manually. - nrelihan_68143
Nimbostratus
Hey Peter, thanks for your response. Your correct, in your response to my last question. I did'nt think it through correctly.
So here is my code that I think should work, I need to test it first.
One more thing do I need quotes around this? http://www.fakewebsite.com/ireland/[HTTP::uri]IRELAND POOL when HTTP_REQUEST { if { [string tolower [HTTP::host]] equals "http://www.fakewebsite.com" \ and [string tolower [HTTP::uri]] starts_with "/ireland"} { HTTP::respond 302 Location http://www.fakewebsite.com/[HTTP::uri] } else { HTTP::respond 302 Location http://www.fakewebsite.com/ireland/[HTTP::uri] } } UK POOL when HTTP_REQUEST { if { [string tolower [HTTP::host]] equals "http://www.fakewebsite.com" \ and [string tolower [HTTP::uri]] starts_with "/ireland"} { HTTP::respond 302 Location http://www.fakewebsite.com/[HTTP::uri] } else { HTTP::respond 302 Location http://www.fakewebsite.co.uk/[HTTP::uri] } } - Peter_72728
Nimbostratus
One issue I see at first glance...These expressions will not work because HTTP::host does not contain the protocol...
string tolower [HTTP::host]] equals "http://www.fakewebsite.com" - Peter_72728
Nimbostratus
This looks like it would allow a redirect loop. Why do you need to do anything in this case? The domain is already correct and the "/ireland" context already exists.
if { [string tolower [HTTP::host]] equals "www.fakewebsite.com" and [string tolower [HTTP::uri]] starts_with "/ireland"} {
HTTP::respond 302 Location http://www.fakewebsite.com/[HTTP::uri]
}
I think you only need the logic from the case where the "/ireland" is added....right?
when HTTP_REQUEST {
if { [string tolower [HTTP::host]] equals "www.fakewebsite.com" and not [string tolower [HTTP::uri]] starts_with "/ireland"} {
HTTP::respond 302 Location http://www.fakewebsite.com/ireland/[HTTP::uri]
}
} - nrelihan_68143
Nimbostratus
Sorry Peter I dont follow, what do you mean they dont contain the protocol?
So you think I should leave out the http:// in the statments? - nrelihan_68143
Nimbostratus
yes your code is a lot more efficient than what I had,
The reason why I included that first case was so I wouldnt have the situation where I'd have http://www.fakewebsite.com/ireland/irealnd (ireland twice) after
But your method takes care of this!
Thanks again! - Peter_72728
Nimbostratus
Correct! - The_Bhattman
Nimbostratus
Hi Neil,
Well your code regarding detecting someone's location is based on the Geographic location based an IP address. So it's important to understand that people who use anonymous proxy systems can get around your iRule by appearing from different location. Some organization have gotten around this by simply locking out requests made by proxy systems. However, that would be something that you or your company would need to decide because there some request could be legitimate.
Assuming that your iRule is really a single Irule with 3 seperate events you can optimize code so that all logic is triggered on one event
when HTTP_REQUEST {
set hostname [string tolower [HTTP::host]]
switch -glob [whereis [IP::client_addr]] {
"IE" {
if {$hostname eq "www.fakewebsite.com" } {
switch -glob [string tolower [HTTP::uri]] {
"/" { HTTP::respond 302 Location "http://www.fakewebsite.com/ireland/" }
"/pictures" { HTTP::respond 302 Location "http://www.fakewebsite.com/ireland/pictures" }
}
pool pool-ireland
}
}
"GB" {
if {$hostname eq "www.fakewebsite.com" } {
switch -glob [string tolower [HTTP::uri]] {
"/" { HTTP::respond 302 Location "http://www.fakewebsite.co.uk/" }
"/pictures" { HTTP::respond 302 Location ""http://www.fakewebsite.co.uk/pictures" }
}
pool pool-UK
}
}
Default { pool pool-default }
}
}
I hope this helps
Bhattman - Colin_Walker_12Historic F5 AccountJust out of curiosity, is there a reason you're using HTTP::respond 302 Location rather than just using HTTP::redirect?
Not a big deal, just wondering. Looks good other than that, Bhattman. ;)
Colin - Colin_Walker_12Historic F5 AccountWorth noting: I ask because the HTTP::redirect command issues a 302 by default.
Recent Discussions
Related Content
DevCentral Quicklinks
* 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
Discover DevCentral Connects
