Skip to content

Caching HTML safely on Cloudflare Free or Pro: the bypass-on-cookie Cache Rules recipe

Cloudflare does not cache HTML by default, and the old bypass-on-cookie safety valve was a Business-plan Page Rule. Cache Rules changed that: two rules give you cached anonymous pages and bypassed sessions on any plan, no APO required. Here is the recipe.

Serving HTML from the edge is the single biggest TTFB win Cloudflare offers, and you do not need APO or a Business plan to get it. Cache Rules expressions can match cookies on every plan, which makes the safe pattern — cache anonymous pages, bypass anyone with a session or cart — a two-rule recipe on Free and Pro. The order of those two rules, and the discipline around purging on publish, are what separate “fast site” from “serving one visitor’s cart to another”.

What’s happening

Default Cloudflare behaviour: static extensions (CSS, JS, images, fonts) are edge-cached, HTML is DYNAMIC — every page request rides through to the origin. The layers stack browser → edge → origin caches, and for pages the edge layer is empty until you opt in.

Historically the safe opt-in needed “Cache Everything” plus “Bypass Cache on Cookie”, and that second half was a Business-plan Page Rule — which is why so many Free-plan sites either never cached HTML or cached it dangerously. Modern Cache Rules removed the plan wall for this pattern: the rules expression language matches on http.cookie with contains on every plan, and when several Cache Rules match a request, later rules in the list override earlier ones. Two rules, correctly ordered, reproduce what APO does for WordPress: anonymous visitors get edge HTML, sessions go to the origin. What the recipe does not replicate from APO is automatic purging on publish — that part you wire yourself.

What to check first

  • Traffic shape. The recipe pays off on anonymous-majority sites (content, marketing, catalogues). A members-only portal will bypass nearly everything and gain little.
  • Cookies on anonymous responses. If the origin starts a session on every page view, each response carries Set-Cookie and stays uncacheable by default — fix that first.
  • The session cookie names for your stack: wordpress_logged_in_*, wp_woocommerce_session_*, woocommerce_items_in_cart, PHPSESSID, laravel_session.
  • Plan limits. The recipe uses two of your Cache Rules (10 on Free, 25 on Pro). Note the regex operator matches needs Business or Enterprise — on Free/Pro build expressions from contains, starts_with, and eq.

How to verify

Baseline before the rules — pages should read DYNAMIC:

curl -sI https://example.com/blog/post/ | grep -iE '^(cf-cache-status|age|cache-control|set-cookie)'

After deploying the rules, run the three-step matrix. Anonymous twice (expect MISS then HIT with rising age), then with a session cookie (expect BYPASS):

curl -sI https://example.com/blog/post/ | grep -iE '^(cf-cache-status|age)'
curl -sI https://example.com/blog/post/ | grep -iE '^(cf-cache-status|age)'
curl -sI -H "Cookie: wordpress_logged_in_x=1" https://example.com/blog/post/ | grep -i '^cf-cache-status'

Confirm the cached copy matches an origin-fresh render, so you know no personalized body slipped in before the bypass rule landed:

curl -s https://example.com/blog/post/ | md5sum
curl -s "https://example.com/blog/post/?fresh=$(date +%s)" --resolve example.com:443:203.0.113.10 | md5sum

The fix

  1. Rule 1 — cache anonymous HTML. Caching → Cache Rules → create “Cache HTML”. Expression scoped to your host, excluding admin and API paths:

    (http.host eq "example.com" and not starts_with(http.request.uri.path, "/wp-admin")
      and not starts_with(http.request.uri.path, "/wp-json")
      and not http.request.uri.path contains "wp-login")

    Cache eligibility: Eligible for cache. Edge TTL: override to a modest value — 30 minutes to 2 hours — because publish-time purges, not expiry, will drive freshness. Browser TTL: respect origin, and keep origin HTML at no-cache so browsers always revalidate; visitors then pick up purges immediately.

  2. Rule 2 — bypass sessions and carts, placed after Rule 1. Create “Bypass sessions” with action Bypass cache:

    (http.cookie contains "wordpress_logged_in") or
    (http.cookie contains "wp_woocommerce_session") or
    (http.cookie contains "woocommerce_items_in_cart") or
    (http.cookie contains "comment_author_") or
    (http.request.uri.path contains "/cart") or
    (http.request.uri.path contains "/checkout") or
    (http.request.uri.path contains "/my-account")

    Order is the safety mechanism: because later rules win, the bypass overrides the cache rule whenever both match. Reversing them recreates the logged-in-page leak.

  3. Leave query strings in the cache key for HTML. Search (?s=) and pagination vary the response; the default key handles them correctly. Normalize tracking parameters only, per the query-string cache-key guide.

  4. Wire purge-on-publish. A CMS hook or the official plugin calling the purge API on post save; without it, edits wait out the Edge TTL. Scope the API token to Zone.Cache Purge.

  5. Watch the first week. Analytics → Cache: HTML hit rate climbing, BYPASS share roughly matching your logged-in traffic share. A BYPASS surge means a new cookie appeared on anonymous traffic.

When this fix is wrong

Skip the recipe when most traffic is authenticated, when pages embed per-visitor content server-side (CSRF tokens printed into forms will be shared — move them to a fetch or keep those routes bypassed), or when a WordPress site is better served by APO, which adds automatic purging and is a paid add-on on Free, included from Pro up. The broader decision of which rules pay off on WordPress is covered in Cloudflare Cache Rules for WordPress.

Common mistakes

  • Putting the bypass rule before the cache rule — later rules override, so the cache rule wins and sessions get cached.
  • Writing matches regex expressions on Free/Pro, where the operator is unavailable.
  • Forgetting comment_author_ cookies, so commenters see their own cached moderation notices served to everyone.
  • Caching without a purge hook and calling stale posts a Cloudflare bug.
  • Testing only logged-out, calling it safe, and never running the cookied request that proves the bypass fires — the leak scenario is dissected in Cache Everything serving logged-in pages.

Prevention

Keep the two expressions in version control alongside a test script that runs the anonymous/cookied matrix after every rules change. Re-run it after plugin installs — new plugins mean new cookies. Review the Cache Rules documentation and the rules language reference when adding operators, and audit hit rate monthly so regressions surface as graphs, not complaints.

Two rules, correct order, purge on publish — that is the whole trick, and it holds on any plan. Cache layers that fight each other cost real revenue — Stack Harbor tunes the whole stack, edge to origin, under Managed Cloud Ops; book an infrastructure review.