Wednesday, May 22, 2024

GeoLocation Redirection in AEM as a Cloud

 In this post, we will explore how to enable GeoLocation redirection in AEM as a Cloud Service.

Sometimes, we may need to enable visitor country-based redirects to direct users to the appropriate page when they access a domain. Multiple approaches are possible to handle geolocation redirection. For example, client-side redirects using the Google Geocoder API or other Geocoder APIs can identify the visitor’s country. Additionally, server-side redirects can be enabled, such as using Apache in conjunction with geocoder services. Most CDN services also provide geo headers that capture the visitor’s country, which can be used to enable redirects.

Let’s now explore different approaches in AEM as a Cloud:

Option 1: Redirect through Apache Using CDN Geo Country Header

AEM as a Cloud uses Fastly CDN out of the box (OOTB). The CDN provides geo headers, such as x-aem-client-country, with every request, supplying an Alpha-2 country code (e.g., US, AR, etc.). This header can be utilized in Apache (Dispatcher) to redirect users to the appropriate country-specific URL. For instance, if a user’s request originates from the US for the domain www.test.com, they can be redirected to the corresponding country-specific homepage URL. To prevent the caching of redirects in the CDN and browser, caching should be disabled for the root path with max-age=0, no-cache, and no-store.

RewriteCond %{REQUEST_URI} ^/$
RewriteCond %{HTTP:x-aem-client-country} ^US$
RewriteRule ^.*$ https:/
/www.test.com/us/en/home.html.html [R=301,L]

Option 2: Redirect through CDN Using clientCountry

Another option is to handle the redirect directly through the CDN using the clientCountry header. The AEM as a Cloud service OOTB CDN now enables multiple capabilities that can be managed by the customer, such as origin selectors, request and response transformation, etc. For more details, refer to A Deep Dive into CDN Capabilities Within AEM as a Cloud | by Albin Issac | Tech Learnings | May, 2024 | Medium..

The latest addition to these capabilities is client redirects. The CDN now allows customers to configure different URL redirects like 301 and 302. This client redirect feature can be combined with the existing geo-location capability to redirect users to country-specific URLs. Note that this client redirect feature is not yet generally available. To join the early-adopter program, email [email protected].

You can add the following rule configuration to the cdn.yml file and add additional rules to meet your criteria:

experimental_redirects:
rules:
- name: country-redirect-us
when:
allOf:
- { reqProperty: clientCountry, equals: "US" }
- { reqProperty: domain, equals: "www.test.com" }
- { reqProperty: path, equals: "/" }
action:
type: redirect
location: https://www.test.com/us/en/home.html

- name: country-redirect-ca
when:
allOf:
- { reqProperty: clientCountry, equals: "CA" }
- { reqProperty: domain, equals: "www.test.com" }
- { reqProperty: path, equals: "/" }
action:
type: redirect
location: https://www.test.com/ca/en/home.html

- name: country-redirect-default
when:
allOf:
- { reqProperty: domain, equals: "www.test.com" }
- { reqProperty: path, equals: "/" }
action:
type: redirect
location: https://www.test.com/default/en/home.html

This configuration sets up rules to redirect users based on their country. For example, users from the US accessing www.test.com will be redirected to the country-specific URL https://www.test.com/us/en/home.html, and users from Canada accessing www.test.com will be redirected to the country-specific URL https://www.test.com/ca/en/home.html. The default rule sends visitors from the rest of the countries to the default website URL https://www.test.com/default/en/home.html. You can add more rules to the cdn.yml file to meet other redirection criteria.

Please note that since the redirect is an experimental feature, experimental_redirects: is used in the configuration. The experimental_ prefix should be removed once this capability becomes generally available (GA).

No comments:

Post a Comment