In this tutorial, we’ll walk through the process of configuring AEM Dispatcher to cache content based on the user’s country using data passed by Cloudflare CDN. By following these steps, you can optimize content delivery and enhance user experience by serving cached content tailored to the user’s location.

Prerequisites: Link to heading

  1. AEM site proxied through Cloudflare.
  2. Ensure Cloudflare passes the CF-IPCountry header containing the country code.
  3. Basic knowledge of Apache configuration and AEM Dispatcher setup.

Steps: Link to heading

1. Configure Cloudflare CDN: Link to heading

Ensure that your AEM site is properly configured to be proxied through Cloudflare. Cloudflare should pass the CF-IPCountry header containing the user’s country code with each request.

2. Rewrite URLs in Apache Configuration: Link to heading

Modify your Apache configuration to rewrite URLs based on the user’s country. Add the following RewriteRule directive:

RewriteRule ^/(.*)$ /content/projectname/$1.%{HTTP:CF-IPCountry}.html [PT,L]

This rule appends the country code to the end of the requested URL before it reaches AEM, ensuring that AEM can serve country-specific cached content.

3. Update Dispatcher Filters: Link to heading

To allow Dispatcher to cache content based on country-specific URLs, update the filters.any file. Add the following rule to whitelist requests with country-specific selectors:

/0104 { /type "allow" /method "GET" /extension "html" /selectors '[A-Z]{2}' /url "/content/*" }

This rule allows caching of HTML content with two-letter country code selectors in the URL path.

4. Update AEM Mapping: Link to heading

Update the AEM mapping to handle country-specific URLs. Navigate to etc/map or wherever your sling:Mapping configuration is stored, and update the following properties for your domain:

  • sling:internalRedirect: Set this as a String array (String[]) with the value /content/projectname/$1.html. This ensures that AEM serves the correct content for the requested URL without the country code.

  • sling:match: Set this as a string with the value content/projectname/(.*)\\.(.*)\\.html$. This pattern matches URLs with the country code appended.

Notes: Link to heading

  • Cache Invalidation: Keep in mind that activating a page will invalidate all country caches since the cache format includes the country code. Plan your cache invalidation strategy accordingly.

  • Outgoing/Reverse Mapping: Depending on your setup, outgoing or reverse mapping may not require updating as the focus here is on incoming requests and caching.

By following these steps, you can leverage AEM Dispatcher along with Cloudflare CDN to efficiently cache and serve country-specific content, optimizing performance and improving user experience for your global audience.