Backo
Backo

Reputation: 18881

How to redirect users to a subdomain based on his/her current location?

I am using Rails 3.1.1 and I would like to redirect users, for example, from the U.S.A. to a proper subdomain us.site.com (this one is hosted on the same server as site.com). I know that I can localize a user by his/her IP address but how can I do that so to redirect he/she to the proper subdomain? There is a technique/gem to geo-locate user IPs and then handle redirection?

P.S.: Maybe, for performance reasons, I should use middlewares...

Upvotes: 1

Views: 940

Answers (2)

ian
ian

Reputation: 12251

https://rubygems.org/gems/rack-geoipcity is a rack middleware gem I've published which you could use, or just use the GeoIP gem in your controllers.

With the rack-geoipcity, you would query the X-headers it adds in and make a decision based on that. Something like:

if headers['X_GEOIP_COUNTRY_CODE'] == "IN"
  redirect "/india"
end

though I don't currently use Rails, so it might be slightly different.

There are plenty to choose from if you don't fancy using the MaxMind db.

Upvotes: 1

sarnold
sarnold

Reputation: 104080

One approach that I've used happily in the past is to perform the geolocation lookup via DNS before users connect to the service; this way, they automatically connect to the server nearest them, you get cheap and easy load balancing and ability to remove servers from active use as you need, and individual sites can go down without influencing other sites.

OFTC uses a self-written oftcdns tool to provide users with nearest servers. During the time I was an administrator on the OFTC network, this tool was a drastic improvement over running a simpler Bind-based DNS server that did not provide geo-location features and complicated bringing servers in and out of the rotation.

Wikipedia uses PowerDNS with a geobackend to provide their geo-ip services. PowerDNS is definitely well-tested, high-demand-capable tool.

Upvotes: 1

Related Questions