2

How would I approach doing a URL redirection that has the following effect:

Website: example.com

If users come from an Indian IP address: example.com -> india.example.com

peso
  • 21
  • 2
  • I have a couple of solutions in mind, but first... Can you elaborate on your needs a little bit more? How many options are there for countries? Are the different countries on different servers? Where is the main/primary site hosted? What about unmatched servers? – Michael - sqlbot Oct 15 '17 at 17:44

1 Answers1

4

Route53 with location based routing would be the typical way to handle location based things. It won't change request domains, but it will route requests to the nearest region.

To achieve that you want, CloudFront can add a header that says where the query is coming from. Based on that you can have a web server do a redirect. Documentation is here.

Using Lambda@Edge you can redirect users to a country specific URL (solution provided by Michael in the comments). More info here.

Next option is to subscribe to a geolocation database. Use a PHP script to decide on location based on source IP and forward accordingly.

Tim
  • 31,888
  • 7
  • 52
  • 78
  • Tim, I am very interested in the Route53 feature that adds a header for geolocation (I was not aware of this feature). However, I cannot find this information from your link, nor from a Google search. Can you help with a link please? – John Hanley Oct 15 '17 at 16:52
  • I may have read that documentation incorrectly. I'll have another look around and update my answer some time today. – Tim Oct 15 '17 at 17:31
  • @Tim not sure what you're thinking of, but Route 53 definitely can't add headers. – Michael - sqlbot Oct 15 '17 at 17:41
  • I was thinking of CloudFront - answer updated. I found the announcement blog, but I can't find the detail in the documentation. It might be there, it might not. This might be a case where experimentation is required. – Tim Oct 15 '17 at 17:43
  • It's documented under [Configuring CloudFront to Cache Objects Based on the Location of the Viewer](http://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/header-caching.html#header-caching-web-location). CloudFront has to cache based on what it specifically forwards to the origin, so the header is only sent to the origin if you add it to the Whitelist. This is the solution I was preparing to post, but I'll leave it to you. – Michael - sqlbot Oct 15 '17 at 19:52
  • Additionally, there's example code for [Redirecting Viewer Requests to a Country-Specific URL](http://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/lambda-examples.html#lambda-examples-redirect-based-on-country), which handles the redirect using Lambda@Edge. This capability was added to Lambda@Edge after the initial release. See [Lambda@Edge Now Provides Access to Query String Parameters, Country and Device Type Headers](https://aws.amazon.com/about-aws/whats-new/2017/10/lambda-at-edge-now-provides-access-to-query-string-parameters-country-and-device-type-headers/). – Michael - sqlbot Oct 15 '17 at 19:58
  • Also note that while this works very well, you can't effectively test this by changing your apparent location with Tor. If CloudFront's geolocation database thinks you're using an anonymous proxy, `CloudFront-Viewer-Country` is not added to the request. Formerly it was set to `A1` for anonymous proxies, but that no longer appears to be the case, and references to this behavior appear to have been removed from the documentation. – Michael - sqlbot Oct 15 '17 at 20:22
  • 1
    Thanks for that @Michael-sqlbot, I've incorporated those links into my answer. – Tim Oct 15 '17 at 21:38