Response headers can be notoriously difficult to change. Particularly if you’re using a CMS like WordPress, it’s often a mystery where certain response headers are coming from. Cache-control is a great example of where many WordPress developers have torn their hair out in frustration, trying to figure out which plugin or .htaccess rule is setting it. Luckily, with Cloudflare, you can now easily change response headers for any subset of your URLs which just a few clicks.
Previously Used Workers to Set Response Headers
Before this tool became fully operational, I recommended Cloudflare workers to set response headers. A great use case was the X-Robots-Tag response header, which Google now requires for all “noindex” pages. I wrote an earlier tutorial on how to add an X-Robots-Tag response header to affiliate links using Cloudflare workers. This was the solution I used on my site WP-Tweaks.com.
Unfortunately, Workers came with some drawbacks. For one, you only got a limited number of worker calls for free. The free limit is generous, but you never know when you might hit the limit. The second (more serious) limitation is that the workers interfere with my bulk redirects, so I can either use workers for the X-Robots-Tag or give up Cloudflare’s bulk redirects.
But thanks to transform rules, I don’t need to make that choice anymore.
How to Use Cloudflare Transform Rules to Set Response Headers
To set the response header, transform rules on Cloudflare, log in and click “Rules,” and then Transform rules as shown here:
This will open the “Transform Rules” configuration screen, where you can modify the request headers, rewrite the URL, or change the response headers. We’re interested in the response headers, so click “Create Transform Rule” and choose “Modify Response Header,” as shown here:
In the response header modification screen, configure your response headers like this:
In the above screenshot, I’ve configured all affiliate links with the URL pattern “/recommends/” to have an X-Robots-Tag. I’ve used Cloudflare’s URI matching field to match a set of URLs and the “Set static” response header because I just need to set a simple value with no additional rules.
Save your changes, and you’re done!
Cloudflare Transform Rules Free Tier Limits
For the Cloudflare free tier, you get 10 free transform rules. This limit is more than enough for a free account, and it’s doubtful you’ll ever need to use more than that. Pro accounts get 25 rules. This includes request header changes and URL rewrites, but even so, it’s more than most websites need.
The benefit of using workers is that there’s no traffic limit to which these rules apply. As long as you stay below the total number of transform rules, you’re good, unlike with workers, where you need to keep an eye on the number of times they execute.
Use Transform Rules for cache-control As Well
I find using transform rules works well for complicated settings like cache control. Instead of using a WordPress plugin and adding to the burden of your site, just use Cloudflare’s transform rule for a set of websites, so you don’t need to worry about systems not caching your pages. I’d written earlier about the pros and cons of advertisements on your site. Ezoic is one of the most popular ad networks, and their systems won’t cache content to speed things up if you have an incorrectly configured cache-control or “Vary” header. You can use Cloudflare transform rules to solve all these problems.
Cloudflare keeps improving its products and offering new services. If QUIC.cloud aims to be a proper competitor, they’d better catch up fast!
I’m a NameHero team member, and an expert on WordPress and web hosting. I’ve been in this industry since 2008. I’ve also developed apps on Android and have written extensive tutorials on managing Linux servers. You can contact me on my website WP-Tweaks.com!