Skip to content
weLabsweLabsStorefront Playbook
Live demoQuickstart

Storefront setup

Media on a Cloudflare R2 custom domain

Serve the store's images from an R2 custom domain, cached for a year in browsers and at Cloudflare's edge, instead of the rate-limited r2.dev address.

When WordPress offloads its media to a Cloudflare R2 bucket, the images are public at the bucket’s r2.dev address by default. That address is rate-limited, Cloudflare marks it as not for production, and it sends no Cache-Control header. As a result, shoppers’ browsers download every image again on each visit, and PageSpeed fails Use efficient cache lifetimes. This page moves the media to a custom domain on the bucket, gives every image a year-long cache lifetime in shoppers’ browsers, and keeps a copy at Cloudflare’s edge for the same year. The setup has no extra Cloudflare cost: every feature it uses is on the Free plan.

Each step is written twice: as dashboard clicks for a person, and as commands for a script or an AI agent. The commands come with an expected result to check against.

  • WordPress offloads its uploads to an R2 bucket with Advanced Media Offloader, and the images currently load from https://pub-….r2.dev/….
  • The domain you will use is a zone in the same Cloudflare account as the bucket. R2 refuses a zone from any other account, even one you also manage. A DNS host that edits a different Cloudflare account, such as FlyWP’s DNS integration, can’t be used for this.
  • You can edit the bucket and the zone’s rules in the Cloudflare dashboard, or you have an API token for that account and zone with R2 Edit, Zone Rulesets Edit, Zone > Cache Rules > Edit and Zone Settings Edit.
  • You can run WP-CLI on the WordPress server, or reach wp-admin as an administrator.

Replace these placeholders everywhere below.

Placeholder Example Where to find it
ACCOUNT_ID 0123456789abcdef0123456789abcdef Dashboard URL, or Account home → account menu → Copy account ID
BUCKET wp-media R2 Object Storage → the bucket WordPress uses (also ADVMO_CLOUDFLARE_R2_BUCKET in wp-config.php)
ZONE example.com The zone that will hold the media subdomain
ZONE_ID fedcba9876543210fedcba9876543210 The zone’s Overview page, API box
MEDIA_HOST media.example.com A subdomain of ZONE with no DNS record yet
OLD_URL https://pub-0123abcd.r2.dev The bucket’s Settings → Public Development URL

1. Connect the custom domain to the bucket

Section titled “1. Connect the custom domain to the bucket”

Cloudflare creates the subdomain’s DNS record itself. Don’t add a CNAME by hand first: an existing record makes the connection fail.

  1. Go to R2 Object Storage, open the bucket, then open Settings.
  2. Under Custom Domains, select Add.
  3. Enter MEDIA_HOST and select Continue.
  4. Check the preview: a CNAME named after the subdomain, pointing at the bucket. Select Connect domain.
  5. Wait until the domain’s Status changes from Initializing to Active, usually a minute or two. Access shows Enabled.

2. Give the media a year-long cache lifetime

Section titled “2. Give the media a year-long cache lifetime”

A Response Header Transform Rule on the zone adds Cache-Control to every successful response from MEDIA_HOST. A year with immutable is safe because WordPress never reuses an upload’s file name: a re-upload or an edited image gets a new name. Limiting the rule to status 200 means a missing file is never cached for a year.

  1. Open the zone ZONE, then Rules → Overview → Create rule → Response Header Transform Rule.

  2. Rule name: Media cache lifetime (MEDIA_HOST).

  3. Choose Custom filter expression, select Edit expression and paste:

    (http.host eq "MEDIA_HOST" and http.response.code eq 200)
  4. Under Then…, choose Set static. Set Header name to Cache-Control and Value to public, max-age=31536000, immutable.

  5. Check that Rule validation passed appears, then select Deploy. The rule is listed as Active.

Step 2 changes only what shoppers’ browsers are told. Cloudflare decides how long its own copy lives before response header rules run, so a Cache-Control set by a Transform Rule doesn’t change the edge. R2 sends no Cache-Control of its own, so without this step Cloudflare keeps each image for its default of 2 hours. After that, the next request in each Cloudflare data centre checks back with R2 (cf-cache-status: REVALIDATED), which took 0.6–0.75 seconds on the demo store against about 0.15 seconds for a cached copy. The hero image is often the request that pays this, and it is the page’s Largest Contentful Paint.

Two settings fix it:

  • A Cache Rule keeps every image on MEDIA_HOST at the edge for a year, whatever R2 sends. Error responses (status 400 and above) are not stored, so a file requested before it was uploaded isn’t stuck as a 404.
  • Smart Tiered Cache lets a data centre that doesn’t have an image fetch it from another Cloudflare data centre near the bucket, instead of from R2.
  1. Open the zone ZONE, then Caching → Cache Rules → Create rule.

  2. Rule name: Media: cache at the edge for a year (MEDIA_HOST).

  3. Choose Custom filter expression, select Edit expression and paste:

    (http.host eq "MEDIA_HOST")
  4. Under Cache eligibility, choose Eligible for cache.

  5. Under Edge TTL, select Add setting, choose Ignore cache-control header and use this TTL, and pick 1 year.

  6. Under Status code TTL, select Add status code setting. Set Scope to Greater than or equal, Status code to 400, and Duration to No store.

  7. Check that Rule validation passed appears, then select Deploy.

  8. Go to Caching → Tiered Cache. Turn on Tiered Cache Topology and make sure Smart Tiered Cache is selected. The topology shows Active.

Change only the public domain Advanced Media Offloader writes into image URLs.

  1. Open the Advanced Media Offloader settings and the Cloudflare R2 section.
  2. In the field that holds the bucket’s public URL (it shows OLD_URL today), enter https://MEDIA_HOST.
  3. Leave the option that appends the bucket name to the domain off: the custom domain already serves just this bucket.
  4. Save.

Media URLs that WordPress builds, such as product images and the media library, change right away. Check with the REST API:

Terminal window
curl -s "https://cms.example.com/wp-json/wp/v2/media?per_page=1&_fields=source_url"
# → [{"source_url":"https://MEDIA_HOST/2026/10/…"}]

Some settings save an image’s full URL instead of its ID, for example a logo in the Companion plugin’s settings, and so does content written in the editor. These still say OLD_URL. Find them first, then replace them:

Terminal window
wp search-replace "OLD_URL" "https://MEDIA_HOST" --all-tables --dry-run --report-changed-only
wp search-replace "OLD_URL" "https://MEDIA_HOST" --all-tables --report-changed-only

Expected: the dry run lists the tables and the number of replacements; the second run makes them. wp search-replace handles serialized PHP values safely.

6. Rebuild the storefront’s cached pages

Section titled “6. Rebuild the storefront’s cached pages”

The storefront’s pages were built with the old URLs. Rebuild them so they pick up the new ones:

StoreFrontSettingsCaching

Select Purge and rebuild everything. The rebuild takes a few minutes, and the status line under the button shows its progress. The old r2.dev URLs keep working in the meantime, so nothing breaks.

Run these checks in order. An agent should treat any mismatch as a failure and stop.

Terminal window
IMG="https://MEDIA_HOST/2026/10/example.jpg" # any image path from the bucket
get() { curl -s -o /dev/null -D - "$1" | grep -iE '^HTTP|cache-control|cf-cache-status|^age'; }
# 1. The domain serves the bucket, with the browser cache lifetime. Run it three times.
get "$IMG"; get "$IMG"; get "$IMG"
# → HTTP/2 200
# → cache-control: public, max-age=31536000, immutable
# → cf-cache-status: HIT (from the second request on; MISS or REVALIDATED is fine on the first)
# → age: a number that grows between runs
# 2. A missing file isn't cached.
get "https://MEDIA_HOST/does-not-exist.jpg"
# → HTTP/2 404, cf-cache-status: BYPASS, and no "immutable"
# 3. The storefront now uses the new domain (after the rebuild).
curl -s "https://shop.example.com/" | grep -c "MEDIA_HOST" # → more than 0
curl -s "https://shop.example.com/" | grep -c "r2.dev" # → 0

A HIT only shows what one data centre holds: the one nearest the machine running curl. Check 1 proves the rule works. It can’t prove that every data centre already holds every image.

Then re-run PageSpeed: Use efficient cache lifetimes should no longer list the media.

A summary of the procedure as rules:

  • Inputs: ACCOUNT_ID, BUCKET, ZONE_ID, MEDIA_HOST, OLD_URL, a WordPress host with WP-CLI, and the storefront URL.
  • Two rules, two jobs: the Transform Rule (step 2) sets the browser’s cache lifetime; the Cache Rule (step 3) sets Cloudflare’s. One doesn’t replace the other.
  • Preconditions: ZONE is in the bucket’s account; MEDIA_HOST has no DNS record yet; the bucket matches ADVMO_CLOUDFLARE_R2_BUCKET.
  • Order: 1 → 2 → 3 → 4 → 5 → 6. Steps 1–3 are safe to run at any time. Run 4–6 only after checks 1 and 2 in Verify pass for MEDIA_HOST.
  • Never: change the R2 Endpoint or the bucket’s keys; PUT over an existing http_response_headers_transform or http_request_cache_settings ruleset; disable the r2.dev URL before step 6 has finished and check 3 passes; judge caching from a HEAD request (curl -I).
  • Ask a human before: the real (not dry-run) search-replace, and Purge and rebuild everything. Both change a live store.
  • Done when: all three checks in Verify match.
Symptom Cause Fix
“That domain was not found on your account” when adding the domain The zone is in another Cloudflare account Use a zone in the bucket’s account, or add the zone to that account first
Connecting fails because a record exists A DNS record for MEDIA_HOST is already there Delete it, or choose another subdomain
No cache-control on images from MEDIA_HOST The rule’s host is misspelled, or the rule isn’t Active Check the expression under Rules → Overview
cf-cache-status: DYNAMIC on every request The check used curl -I, a HEAD request, which Cloudflare never caches Check with a GET, as in Verify
An image is sometimes slow, with cf-cache-status: REVALIDATED Cloudflare’s copy expired after its 2-hour default: the step 3 Cache Rule is missing, misspelled or not deployed Check Caching → Cache Rules; the rule’s expression must name MEDIA_HOST
The storefront still shows r2.dev images Its pages were cached before the switch, or a URL is saved as text Run step 5’s dry run, then Purge and rebuild everything
New uploads fail to offload The Endpoint was changed to the custom domain Put back https://ACCOUNT_ID.r2.cloudflarestorage.com/
Storefront Playbook · Built by weLabsFeaturesFAQTalk to us