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
- The_Bhattman
Nimbostratus
This was a cut paste part. Was trying to retain as much of the poster Neil code. However, yes you should be able to use HTTP::redirect all the same, unless the poster Neil had a different intention for it.
Bhattman - Colin_Walker_12Historic F5 AccountI knew you were cutting and pasting, I just didn't word my question very well. I was asking the OP why he was using the HTTP::respond. I figured you were just keeping things the way they were, Bhattman. ;)
Colin - nrelihan_68143
Nimbostratus
Hey guys, no theres no particular reason why I'm using HTTP::respond 302, if you think I should use HTTP::redirect, I'll go with that, I saw this, in some code today.
Bhattman, the reason why I was splitting up the code was to make it simplier for myself. I was using the global traffic irules to call the DNS_request funtion and then directing the traffic to the appropriate pools. Then running the other irules in the individual pools.
I didnt really think of running all the rules in the global section. I imagine its alot more efficient this way.
One thing about your code, you dont use the DNS_request function? Is there need for this?
Do you know of any other ways to determine an IP's location other than DNS? Yeah I dont think locking out requests is a viable option :)
Thanks! - Colin_Walker_12Historic F5 AccountHe's making use of the whereis command just like you were nrelihan, he's just choosing to use it in a different event. The result will be the same, it will still perform a host resolution like you're looking for.
Colin - nrelihan_68143
Nimbostratus
Thanks for the help last week guys, So I decided to put the rules into one, as it was mentioned above.
Have to yet test the code but could you have a quick browse and tell me if theres anything wrong with the syntax?
Thanks again!when HTTP_REQUEST { set hostname [string tolower [HTTP::host]] set uri [string tolower [HTTP::uri]] switch -glob [whereis [IP::client_addr]] { "IE" { if {$hostname eq "www.quinn-direct.com" and not $uri starts_with "/ireland"} { HTTP::redirect http://www.quinn-direct.com/ireland/[HTTP::uri] pool gpool-qdi-ie-quinn-direct.com-ie } } "GB" { if {$hostname eq "www.quinn-direct.com" and not $uri starts_with "/ireland" } { HTTP::redirect http://www.fakewebsite.co.uk/[HTTP::uri] pool gpool-qdi-ie-quinn-direct.com-uk } } Default { pool gpool-qdi-landing-cavan-http } } } - Peter_72728
Nimbostratus
I don't think you need -glob on that switch statement. You are doing an exact match on "IE" and "GB".
Also, I think you want a lowercase "d" in "default".
See these for more information about the switch command
http://devcentral.f5.com/wiki/defau...witch.html
http://devcentral.f5.com/Tutorials/...witch.aspx
- nrelihan_68143
Nimbostratus
Yeah your right with the default! The reason why I did the -glob was because I saw it in other code.
Thanks,
Neil - Peter_72728
Nimbostratus
My first response was after I looked at the syntax...now I'm looking at the logic and I'm not sure it will work.
When a request comes in that doesn't have the "/ireland" prefix, you want to redirect it. But in your sample code, you are setting the pool as well. Then, when a request comes in that already has the "/ireland" prefix, you don't set a pool.
I think you want to change it to something like this...
if {$hostname eq "www.quinn-direct.com" and not $uri starts_with "/ireland"} {
HTTP::redirect http://www.quinn-direct.com/ireland/[HTTP::uri]
}
else {
pool gpool-qdi-ie-quinn-direct.com-ie
} - nrelihan_68143
Nimbostratus
Ok yeah that makes sense, thanks.
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
