Skip to content
Tutorials Center
My learning

Topics

Lesson 11 of 36 ยท CDN and Caching

Cloudflare Cache Rules: Cache Everything, Bypass and Custom TTLs

Use Cloudflare Cache Rules to cache HTML, bypass the cache for admin and API paths, set Edge and Browser TTL, and replace old Page Rules safely.

Defaults and origin headers get you far, but real sites need exceptions: cache a static blog's HTML, never cache /admin, keep images for a month, ignore tracking parameters. Cache Rules express those decisions as ordered rules in the dashboard (Caching > Cache Rules) or through the API. They replace the older Page Rules for caching.

Anatomy of a rule

Every rule is a condition plus settings.

  • When incoming requests match... a filter built from request fields: hostname, URI path, file extension, query string, header, cookie, country and so on.
  • Then... what to do:
    • Cache eligibility: Bypass cache or Eligible for cache (cache even HTML and JSON).
    • Edge TTL: use the origin's Cache-Control when present, ignore it and use your value, or only apply a TTL when the origin sends none.
    • Browser TTL: respect the origin or override it.
    • Cache key: change what makes two requests "the same object" (next lesson).
    • Serve stale content while revalidating, respect strong ETags, origin error page pass-through and a few more switches.

Rules run in order from top to bottom, and when several rules match, settings from later rules override the same setting from earlier ones. A common layout is: a broad rule first, then narrower exceptions below it.

Example 1: cache a static blog

TEXT
Rule name: Cache blog pages
When: Hostname equals "blog.example.com"
Then:
  Cache eligibility: Eligible for cache
  Edge TTL: Ignore cache-control header and use this TTL = 4 hours
  Browser TTL: Override = 5 minutes

Each publication then needs a purge, or you accept up to four hours of delay.

Example 2: never cache sensitive paths

TEXT
Rule name: Bypass admin and API
When: URI Path starts with "/wp-admin" or URI Path starts with "/api/"
Then: Cache eligibility: Bypass cache

Put bypass rules after broad "cache everything" rules, so they win. Cookie-based bypass is common on CMS sites: Cookie contains "wordpress_logged_in" then bypass, so editors see fresh pages while anonymous visitors get cached ones.

Example 3: long TTL for static assets

TEXT
Rule name: Static assets
When: File extension in {css js woff2 png jpg webp svg}
Then:
  Edge TTL: Ignore origin, 30 days
  Browser TTL: Override = 1 year (only safe with fingerprinted names)

Expressions

Behind the form builder is the same rules language used by the WAF (chapter 4). In the expression editor the first rule above looks like this:

TEXT
(http.host eq "blog.example.com")

and the second:

TEXT
(starts_with(http.request.uri.path, "/wp-admin")) or (starts_with(http.request.uri.path, "/api/"))

Switching to the editor gives you and, or, not, in {...} sets and functions like lower() and starts_with().

Rule order stand-in

The simulator below evaluates a list of cache rules for several requests, applying later overrides. It supports a few conditions only, but it shows the "last matching setting wins" behavior that confuses beginners.

JS Stand-in: Cache Rules engine

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Reorder the rules and notice how the results change. In particular, move "Bypass admin" above "Cache blog HTML": the admin path would then become cacheable again, because a later rule overrides the earlier bypass. This is the most common cache-rule bug.

Cache Everything and its dangers

Setting a path as Eligible for cache is the modern "Cache Everything". It makes Cloudflare store responses the origin might have meant to be personal. Ensure the cached URLs:

  • Contain no per-user data (names, carts, tokens, CSRF tokens in the HTML).
  • Do not depend on cookies or the Authorization header (bypass when those exist).
  • Are not rendered differently by language or device on the same URL; use different URLs or add the variation to the cache key.

A caching incident where one visitor sees another's account page is a classic, serious mistake. Test with curl using two different cookies and compare the responses.

Other rule families

Cache Rules are one of several Rules products, all sharing the same expression language: Redirect Rules (301/302 by pattern), URL Rewrite and Header Transform Rules, Origin Rules (change the origin host/port or Host header for a path), Configuration Rules (toggle zone settings such as security level per path) and Compression Rules. Page Rules, the older all-in-one tool, still work on many zones but are being phased out; migrate each Page Rule to the specific rule type that replaces it.

Practical workflow

  1. Write down which paths are static, semi-static and private.
  2. Create rules from broad to specific, bypasses last.
  3. Test with curl -sI twice and read cf-cache-status and age.
  4. Watch the cache hit ratio in analytics for a few days.
  5. Document the rules; a dashboard click is easy to forget.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does 'Eligible for cache' on an HTML path do?

    It opts the response in to edge caching.
  2. 2. Two rules match a request and both set Edge TTL. Which value is used?

    Rules are evaluated top to bottom and later settings override earlier ones.
  3. 3. Where should a bypass rule for /admin be placed relative to a broad 'cache everything' rule?

    Placing it after lets it override the broader eligibility.
  4. 4. Which is a danger of caching HTML for logged-in pages?

    Cached personal pages can leak between visitors, so bypass them on cookies or auth headers.

Summary

  • Cache Rules combine a request filter with eligibility, TTL and cache-key settings, evaluated top to bottom.
  • Use them to cache HTML, bypass private paths and set Edge and Browser TTLs.
  • Later rules override earlier ones; put bypass rules last.
  • Test with curl, never cache personal content, and migrate old Page Rules.

Next: Tiered Cache and Cache Reserve