Redirecting 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

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.

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](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]

}
}

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”

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]

}

}

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?

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!

Correct!

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

Just 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. :wink:

Colin

Worth noting: I ask because the HTTP::redirect command issues a 302 by default.

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

I 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. :wink:

Colin

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 :slight_smile:

Thanks!

He’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

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 }

     }

}

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

Yeah your right with the default! The reason why I did the -glob was because I saw it in other code.

Thanks,

Neil

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

}

Ok yeah that makes sense, thanks.