Cloudflare Cache Rules are an advanced caching management system that allows you to determine which resources of your website will be cached and for how long. Cache settings, which were previously only made through Page Rules, have now become much more flexible with Cache Rules, which is a more detailed and rule-based system.
In this article, we explain in detail the Cloudflare Cache Rules feature, how to create rules, for which situations it is useful, and we present practical examples step by step.
What are Cache Rules?
Cache Rules allow you to determine what will be cached and what will not be cached on Cloudflare's edge servers. Thanks to this system:
-
You can define different cache behaviors for specific file types,
-
Specific URL patterns,
-
And custom filter conditions.
It offers the possibility to customize cache control, especially for dynamic sites, media sites, or web applications serving a large number of users.
Where are they used?
-
Caching static content for a long time (CSS, JS, image files)
-
Excluding dynamic content under a specific folder from the cache
-
Activating cache only on specific URLs
-
Applying different TTL rules on a subdomain basis
-
Exempting API endpoints with special parameters from the cache
️ Step-by-Step Cache Rule Creation (Based on the Example in the Image)
1. Enter the Cache Rules Panel
-
Log in to your Cloudflare account
-
Follow the path
Caching > Cache Rules
from the left menu -
Click the "Create rule" button
2. You Can Choose From Ready-Made Templates or Define a Custom Rule
-
Cache Everything: Caches all requests
-
Bypass Cache for Everything: Excludes all requests from the cache
-
Cache Default File Extensions: Applies cache for specific extensions (jpg, css, js, etc.)
3. Create Your Own Rule
-
Give a descriptive name to the "Rule name" field (example:
Cache Static Files
)
4. Determine the Conditions (if incoming requests match...)
-
Select
Custom filter expression
-
For example: You can define filters such as
URI Full
,wildcard
,*.jpg
(http.request.full_uri wildcard "*example.com/static/*")
This example targets all files in the /static/
directory.
5. Next Steps (Then...)
-
Cache eligibility
: Select "Eligible for cache" -
Edge TTL
: How long should it be kept in the cache on the edge server? -
Browser TTL
: How long should it be cached in the browser? -
Serve stale while revalidating
: Serve the old content until it is updated, even if the cache expires -
Origin error page pass-through
: Should errors from the origin be served as is?
6. Saving and Publishing
-
Activate it by pressing the "Deploy" button
-
Alternatively, you can save it as a draft with "Save as Draft"
Tips and Best Practices
-
Create a cache bypass rule for dynamic content (e.g. /login, /cart)
-
Use long TTL values for static content (e.g. 1 month)
-
Use version (v=1.2.3) in file names to avoid the need for CDN cache clearing
-
Check the
Respect strong ETags
andCache key
settings for developers
Things to Consider
-
Cache is not always good. If user-specific data is cached, privacy issues arise.
-
User cart etc. areas should be excluded from the cache in systems such as WooCommerce, WordPress.
-
Remember to clear the Cloudflare cache (Purge Cache) after each change.
Conclusion
Cloudflare Cache Rules is a powerful tool to make performance and resource management as efficient as possible. It offers much more detailed, logical filtering and customization than the old "Page Rules" method. When configured correctly, it both reduces the server load and provides a faster experience for the user.
After creating the rules, remember to analyze them regularly and examine their effects from the Analytics & Logs > Cache
section when necessary.