Point to R2 bucket with a custom domain

Last updated Oct 13, 2025 This tutorial will instruct you how to configure an origin rule and a DNS record to point to an R2 bucket configured with a custom domain.

The procedure will use the following example values:

URL that website visitors will access mycustomerexample.com/images/*
R2 bucket custom domain imagesbucket.example.com

When configuring your R2 bucket's custom domain, use a custom domain that you do not plan to use in production (imagesbucket.example.com in this example).

1. Configure custom domain in your Pages project

  1. In the Cloudflare dashboard, go to the R2 object storage page. Go to Overview ↗
  2. Select your bucket.
  3. On the bucket page, select Settings.
  4. Under Public access > Custom Domains, select Connect Domain.
  5. Enter the domain name you want to connect to — imagesbucket.example.com in this example — and select Continue.
  6. Review the new record that will be added to the DNS table and select Connect Domain.

Your domain is now connected. The status takes a few minutes to change from Initializing to Active, and you may need to refresh to review the status update. If the status has not changed, select the ... next to your bucket and select Retry connection.

To view the added DNS record, select ... next to the connected domain and select Manage DNS.

Note

The domain used must belong to the same account as the R2 bucket.

2. Create origin rule to rewrite host header and override DNS record

In your mycustomerexample.com zone, create an origin rule with the following configuration:

If incoming requests match

Field Operator Value
URI Path wildcard /images/*

If using the Expression Editor, enter the following expression:

(http.request.uri.path wildcard "/images/*")

Set origin parameters

  • Value after Host header > Rewrite to: imagesbucket.example.com
  • Value after DNS record > Override to: imagesbucket.example.com

3. (Optional) Configure URL rewrite

In our example, the URL that website visitors will access starts with /images. However, images stored in the example R2 bucket do not have this initial URL segment.

Use a URL rewrite to remove the /images segment from the URL path. Cloudflare provides a rule template in the dashboard called Rewrite Path for Object Storage Bucket that you can use to configure the required rewrite.

  1. In the Cloudflare dashboard, go to the Rules Overview page. Go to Overview ↗
  2. Select Create rule > URL Rewrite Rule.
  3. Enter a descriptive name for the rule in Rule name.
  4. In If incoming requests match, select Wildcard pattern.
  5. Enter the following value in Request URL:

txt https://<YOUR_HOSTNAME>/images/*

In the current example, the value would be https://mycustomerexample.com/images/*. 6. In Then rewrite the path and/or query, enter the following values under Path:

Target path Rewrite to
[/] images/* [/] ${1}
7. Select Deploy.

Note

Cloudflare provides a rule template in the dashboard called Rewrite Path for Object Storage Bucket that you can use and adapt to configure the URL rewrite rule.

More resources

Was this helpful?

YesNo

On this page

Point to R2 bucket with a custom domain - 图1Docs

{"@context":"https://schema.org","@type":"TechArticle","@id":"https://developers.cloudflare.com/rules/origin-rules/tutorials/point-to-r2-bucket-with-custom-domain/#page","headline":"Point to R2 bucket with a custom domain","description":"This tutorial will instruct you how to configure an origin rule and a DNS record to point to an R2 bucket configured with a custom domain.","url":"https://developers.cloudflare.com/rules/origin-rules/tutorials/point-to-r2-bucket-with-custom-domain/","inLanguage":"en","image":"https://developers.cloudflare.com/rules/origin-rules/tutorials/point-to-r2-bucket-with-custom-domain/og.png?v=ae2f1b264cb85e10","dateModified":"2025-10-13","publisher":{"@type":"Organization","name":"Cloudflare","description":"One platform for your apps, agents, and workforce. Build, secure, and scale without managing infrastructure","url":"https://www.cloudflare.com/","sameAs":["https://github.com/cloudflare","https://www.linkedin.com/company/cloudflare","https://x.com/cloudflare"],"logo":{"@type":"ImageObject","url":"https://developers.cloudflare.com/logo.svg"},"address":{"@type":"PostalAddress","streetAddress":"101 Townsend St","addressLocality":"San Francisco","addressRegion":"CA","postalCode":"94107","addressCountry":"US"},"contactPoint":[{"@type":"ContactPoint","contactType":"Customer Support","url":"https://support.cloudflare.com/","availableLanguage":["English"]},{"@type":"ContactPoint","contactType":"Sales","url":"https://www.cloudflare.com/contact/","availableLanguage":["English"]}]},"isPartOf":{"@type":"WebSite","@id":"https://developers.cloudflare.com/#website","name":"Cloudflare Docs","url":"https://developers.cloudflare.com/"}}


description: Control what and how Cloudflare caches with Cache Rules. title: Cache Rules image: https://developers.cloudflare.com/cache/how-to/cache-rules/og.png?v=660fb03f7dc19f4d


Skip to content

Documentation Index
Fetch the complete documentation index at: https://developers.cloudflare.com/cache/llms.txt
Use this file to discover all available pages before exploring further.