A Transform Rule - Rewrite URL lets you change either the path or the query string in their way from Cloudflare data centers to the origin server. When the path or query string is changed by a Transform Rule, the browser will still display and “think” it is getting the requested URL, while Cloudflare Edge and your website origin will receive and process the modified URL.
When using a Transform Rule, you need to match the final and canonical URL, as changing either path or query string before a redirect is performed can result in unintended consequences. That’s why we always match SSL, Hostname, and URI Path, as well as, if applicable, URI Query String.
Read More
So, if user requests:
http://example.com/old-path/
and that is redirected a few times, first to https://
https://example.com/old-path/
then to www.
https://www.example.com/old-path/
and finally to the /current-path/
https://www.example.com/current-path/
only this final and canonical URL should be modified by the Transform Rule. Otherwise the changed URL will pass along as part of the redirect sequence, which may cause unintended results, such as SEO issues.
Examples of some Transform Rules (Rewrite URL):
1 - Remove query string with analytics parameters
Analytics programs such as Google Analytics and Facebook Pixel may add query parameters to requests going to your origin. When that happens, in many cases the request will generate a new cache key, either in Cloudflare cache or at the origin, reducing the efficiency of your cache policy. With this Transform Rule, we’ll remove such query strings. They are normally not needed in the origin, as the query parameters are interpreted by the browser’s JavaScript engine.
Transform Rule details
(Assuming the canonical URL starts with https://www.example.com
, and query parameter to be removed is Facebook’s fbclid
)
When incoming requests match
Field: SSL
AND
Field: Hostname
Operator: equals
Value: www.example.com
AND
Field: URI Query String
Operator: contains
Value: fbclid
Then
Path: Preserve
Query: Rewrite to Static <LEAVE VALUE EMPTY>
2 - Lower the case of the search query string
Your website has a search feature that is normally accessed via a query string s=<search keyword>
, and search results pages are cached by the origin (and/or by Cloudflare). But users sometimes type in the search keywords in UPPERCASE, or even CamelCase, resulting in unnecessary cache keys for the same content. To avoid this, we’ll use the lower()
function.
Transform Rule details
When incoming requests match
(Assuming the canonical URL starts with https://www.example.com
)
When incoming requests match
Field: SSL
AND
Field: Hostname
Operator: equals
Value: www.example.com
AND
Field: URI Query String
Operator: contains
Value: s=
Then
Path: Preserve
Query: Rewrite to Dynamic
Expression: lower(http.request.uri.query)
3 - Add version as query string parameter to bust the cache of unpurgeable URLs
Despite creating some of the most advanced and forward-looking Web technologies of our time — or perhaps because of that — Cloudflare has a persistent issue where its cache (and its many layers) will at times leave you stuck with one or a few URLs that you simply cannot purge except by resorting to Purge Everything. With these Transform Rules, you’ll add a static query string parameter, such as ver=n
(where n
is the version number), to the unpurgeable URL(s). You can then update the version number each time you need to update the content for these URLs by editing the respective Transform Rule.
Since the URL in case may or not have its own query string, we need to implement two rules, Transform Rule A to add a query string when one is not present, and Transform Rule B to concatenate a pre-existing query string with the added parameter.
Obs.: If your website is running APO for WordPress, you must also create a Cache Rule using the same expression for the “When incoming requests match” section, and a setting of “Eligible for cache.” This will take these URLs out of APO’s cache logic, but keep them cached by Cloudflare (and Tiered Cache, if enabled.)
Transform Rule A details
When incoming requests match
(Assuming the canonical URL starts with https://www.example.com
)
When incoming requests match
Field: SSL
AND
Field: Hostname
Operator: equals
Value: www.example.com
AND
Field: URI Path
Operator: is in
Value: /path-1/ /path-2/ /path-3/…
AND
Field: URI Query String
Operator: equals
Value: <LEAVE EMPTY>
Then
Path: Preserve
Query: Rewrite to Static
Expression: ver=n
Transform Rule B details
When incoming requests match
(Assuming the canonical URL starts with https://www.example.com
)
When incoming requests match
Field: SSL
AND
Field: Hostname
Operator: equals
Value: www.example.com
AND
Field: URI Path
Operator: is in
Value: /path-1/ /path-2/ /path-3/…
AND
Field: URI Query String
Operator: does not equal
Value: <LEAVE EMPTY>
AND
Field: URI Query String
Operator: does not contain
Value: ver=n
[Needed to avoid adding the version parameter multiple times, as a Transform Rule may run more than once depending on several Cloudflare settings.]
Then
Path: Preserve
Query: Rewrite to Dynamic
Expression: concat(http.request.uri.path, "&ver=1")
4 - Remove query strings from the front end of WordPress static websites
Query strings will bust the cache of your HTML, forcing WordPress to regenerate the page from scratch, and that leaves your site vulnerable to random query-string attacks. You can safely remove query strings from your front end if you use the WordPress platform to maintain a static website (not a blog, not ecommerce etc.) Please note: some plugins may need query strings to work properly even in a static website, so you need to test and adjust the rule accordingly.
Transform Rule details
(Assuming the canonical URL starts with https://www.example.com
and your WordPress Permalinks structure to be any variation that ends with a trailing slash)
When incoming requests match
Field: SSL
AND
Field: Hostname
Operator: equals
Value: www.example.com
AND
Field: URI Path
Operator: does not start with
Value: /wp-
AND
Field: URI Path
Operator: ends with
Value: /
AND
Field: URI Query String
Operator: does not start with
Value: s=
Then
Path: Preserve
Query: Rewrite to Static <LEAVE VALUE EMPTY>
See Other Transform Rules Examples From the Documentation: