We use content localization based on timezones on the homepage of our site.
We do this to show data to our visitors for today and the upcoming 7 days based on their local time.
Example: When you visit our homepage at 23:00 from Los Angeles, you will still have data for today. However, those in New York will be served data for tomorrow (Los Angeles time) and 7 days after.
We cache both versions of the homepage on our server and serve these 2 pages based on the timezone the visitor is from. We use Maxmind for that.
We do this server-side so that we can have them cached and there’s no processing going on other than the detection of timezones and loading of the correct cached page.
To use Cloudflare to serve these 2 cached pages from their CDN for the same URL based on the timezone/location of a visitor, we would need to add a Transform Rule > Rewrite URL based on timezones. And Cloudflare isn’t offering that on a Pro plan. It only offers countries/continents (which isn’t precise enough).
So my questions are:
does Cloudflare even offer Transform Rules based on timezones?
should we use some other solution that I am not aware of that Cloudflare has?
There is the field of ip.src.timezone.name but it is only valid for rewrite expression and not filter expressions. I am not aware of any other native solutions, you could write something using workers to use the ip.src.lat and ip.src.lon fields and a 3rd party API to look those up.
But you can use ip.src.city, and a few other geolocation fields that are a bit more precise. Unfortunately, the documentation is not always explicit about which field is restricted by plan level, so you need to try the ones you think might work in your use case.
Make sure your Transform Rule is applied after any expected redirection occurs:
When incoming requests match...
(ssl and http.host eq "example.com" and ip.src.city in {"city 1" "city 2" ...}
Thanks guys. These may exist in higher plans. Need to contact sales for info.
Too bad the documentation doesn’t mention any of that. They do mention here and there that something is only available on higher plans, but pretty inconsistently. So you can’t really tell.
This is not a plan level limitation, as explained by @Cyb3r-Jak3. The field can be used only in the rewrite rule itself, not as part of the “When incoming requests match…” filter. You can use other fields for this filter, such as ip.src.city. Or even write your own
What I am still uncertain about is whether will CF create a separate cache key for each of the rewritten URLs and serve that rewritten URLs from the cache next time OR will that rewritten URL simply replace the cache stored for the original URL matched in in the filter expression??
The idea is right, but the implementation needs fixing:
Dynamic: concat("city=", ip.src.timezone.name)
The query string will be passed on, and since Transform Rules come before Cache, yes, Cloudflare would cache separately each timezone. However, you need to make sure your cache configuration is caching HTML with query strings. You do that with a Cache Rule.
When incoming requests match
URI Query contains "city="
Eligible for cache
etc....
Once that is set, visitors from each timezone will get their separately-cached content. Your origin of course must be ready to respond to those query strings accordingly.
No redirection rules are in place. This rewrite is all I need.
Also, we cache everything except some URLs that are ruled out like the admin area so it should cache these queries fine.
Since CF caches for these need to be purged once per hour, I believe I will need to add a Page Rule for / (homepage) with Edge Cache TTL of 1 hour. I just wonder will that rule set TTL for the homepage or for all these rewritten queries of the homepage (which is what we need)
It would be very challenging to control this via Edge Cache TTL. You should instead write a script/cronjob to purge the page from Cloudflare Cache via API.
No, you don’t. A Cache Rule with “Eligible for cache” setting is equivalent to a Cache Everything Page Rule…
Depends on the filter you created for your Cache Rule. If you use URI Path equals “/”, than it would cache your homepage and all variants of it, one per different query string.