This tutorial will instruct you how to configure an origin rule and a DNS record to point to a Pages deployment with a custom domain.
The procedure will use the following example values:
| URL that website visitors will access | mycustomerexample.com/blog/* |
| Zone domain | mycustomerexample.com |
| Cloudflare Pages subdomain | myblog.pages.dev |
| Cloudflare Pages custom domain | blogmirror.example.com |
When configuring your Pages custom domain, use a custom domain that you do not plan to use in production (blogmirror.example.com in this example).
To add the custom domain to your Pages deployment:
-
In the Cloudflare dashboard, go to the Workers & Pages page.
Go to Workers & Pages ↗ -
Select your Pages project.
-
Go to Custom domains.
-
Select Set up a custom domain.
-
Enter
blogmirror.example.comand select Continue.
When you add the custom domain to your Pages deployment, Cloudflare automatically creates a CNAME DNS record for the custom domain.
In your mycustomerexample.com zone, create an origin rule with the following configuration:
If incoming requests match
| Field | Operator | Value |
|---|---|---|
| URI Path | wildcard | /blog/* |
If using the Expression Editor, enter the following expression:
(http.request.uri.path wildcard "/blog/*")Set origin parameters
- Value after Host header > Rewrite to:
blogmirror.example.com - Value after DNS record > Override to:
blogmirror.example.com
In this example, the URL that website visitors will access starts with /blog. However, the Pages deployment does not have this initial URL segment.
Use a URL rewrite to remove the /blog segment from the URL path.
-
Go to Rules > Overview.
-
Select Create rule > URL Rewrite Rule.
-
Enter a descriptive name for the rule in Rule name.
-
In If incoming requests match, select Wildcard pattern.
-
Enter the following value in Request URL:
https://<YOUR_HOSTNAME>/blog/*In the current example, the value would be
https://mycustomerexample.com/blog/*. -
In Then rewrite the path and/or query, enter the following values under Path:
Target path Rewrite to [ /]blog/*[ /]${1} -
Select Deploy.