|
| 1 | +--- |
| 2 | +updated: 2025-04-12 |
| 3 | +difficulty: Beginner |
| 4 | +pcx_content_type: tutorial |
| 5 | +title: Point to R2 bucket with a custom domain |
| 6 | +products: |
| 7 | + - R2 |
| 8 | + - Origin Rules |
| 9 | +--- |
| 10 | + |
| 11 | +import { Example } from "~/components"; |
| 12 | + |
| 13 | +This example configures an origin rule and a DNS record to point to an R2 bucket configured with a custom domain. |
| 14 | + |
| 15 | +The procedure will use the following example values: |
| 16 | + |
| 17 | +| | | |
| 18 | +| ------------------------------------: | -------------------------------- | |
| 19 | +| URL that website visitors will access | `mycustomerexample.com/images/*` | |
| 20 | +| R2 bucket custom domain | `imagesbucket.example.com` | |
| 21 | + |
| 22 | +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). |
| 23 | + |
| 24 | +## 1. Configure custom domain in your Pages project |
| 25 | + |
| 26 | +1. Log in to the [Cloudflare dashboard](https://dash.cloudflare.com/) and select your account. |
| 27 | +2. Go to **R2 Object Storage** and select your bucket. |
| 28 | +3. On the bucket page, select **Settings**. |
| 29 | +4. Under **Public access** > **Custom Domains**, select **Connect Domain**. |
| 30 | +5. Enter the domain name you want to connect to — `imagesbucket.example.com` in this example — and select **Continue**. |
| 31 | +6. Review the new record that will be added to the DNS table and select **Connect Domain**. |
| 32 | + |
| 33 | +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**. |
| 34 | + |
| 35 | +To view the added DNS record, select **...** next to the connected domain and select **Manage DNS**. |
| 36 | + |
| 37 | +:::note |
| 38 | +The domain used must belong to the same account as the R2 bucket. |
| 39 | +::: |
| 40 | + |
| 41 | +## 2. Create origin rule to rewrite host header and override DNS record |
| 42 | + |
| 43 | +In your `mycustomerexample.com` zone, create an origin rule with the following configuration: |
| 44 | + |
| 45 | +<Example> |
| 46 | + |
| 47 | +**If incoming requests match** |
| 48 | + |
| 49 | +| Field | Operator | Value | |
| 50 | +| -------- | -------- | ----------- | |
| 51 | +| URI Path | wildcard | `/images/*` | |
| 52 | + |
| 53 | +If using the Expression Editor, enter the following expression: |
| 54 | + |
| 55 | +```txt |
| 56 | +(http.request.uri.path wildcard "/images/*") |
| 57 | +``` |
| 58 | + |
| 59 | +**Set origin parameters** |
| 60 | + |
| 61 | +- Value after **Host header** > **Rewrite to**: `imagesbucket.example.com` |
| 62 | +- Value after **DNS record** > **Override to**: `imagesbucket.example.com` |
| 63 | + |
| 64 | +</Example> |
| 65 | + |
| 66 | +## 3. (Optional) Configure URL rewrite |
| 67 | + |
| 68 | +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. |
| 69 | + |
| 70 | +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. |
| 71 | + |
| 72 | +1. Go to **Rules** > **Overview**. |
| 73 | +2. Select **Create rule** > **URL Rewrite Rule**. |
| 74 | +3. Locate the **Rewrite Path for Object Storage Bucket** rule template, and select **Create a rule**. |
| 75 | +4. Update the rule name. |
| 76 | +5. Change the value in **Request URL** to: |
| 77 | + |
| 78 | + ```txt |
| 79 | + https://<YOUR_HOSTNAME>/images/* |
| 80 | + ``` |
| 81 | + |
| 82 | +6. Change the values under **Path** to: |
| 83 | + |
| 84 | + | Target path | Rewrite to | |
| 85 | + | ----------- | ---------- | |
| 86 | + | `images/*` | `${1}` | |
| 87 | + |
| 88 | +7. Select **Deploy**. |
| 89 | + |
| 90 | +## More resources |
| 91 | + |
| 92 | +- [Tutorial: Change URI Path and Host Header](/rules/origin-rules/tutorial/change-uri-path-and-host-header/) |
| 93 | +- [Cloudflare R2: Public buckets](https://developers.cloudflare.com/r2/buckets/public-buckets/) |
| 94 | +- [DNS records](/dns/manage-dns-records/) |
0 commit comments