Last Updated on 15/02/2022 by DataWorld.xyz
Learn how to set up URL forwarding or redirects with Cloudflare.
In this article
- Forwarding examples
- Advanced forwarding options
- Related resources
- Free Cloudflare VPN Secure Your Internet
Set up DNS
On the DNS dashboard, enter the following entries:
I’m not sure if the
www entry is really needed – I created it to be sure, and it at least didn’t break anything
You can define a page rule to forward or redirect traffic to a different URL using the Rules app in the Cloudflare dashboard.
To configure URL forwarding or redirects:
- Log into your Cloudflare account.
- Click the appropriate Cloudflare account for the domain where you want to add URL forwarding.
- Select Rules > Page Rules.
- Under Page Rules, click Create Page Rule. The Create Page Rule for <your domain> dialog opens.
- Under If the URL matches, enter the URL or URL pattern that should match the rule.
- Next, click Add a Setting.
- Choose Forwarding URL from the drop down menu.
- Click Select Status Code and choose 301 (Permanent Redirect) or 302 (Temporary Redirect).
- Enter the destination URL.
- To finish, click Save and Deploy.
Imagine you want site visitors to easily reach your website for a variety of URL patterns. For instance, the Page Rule URL patterns *www.example.com/products and *example.com/products match:
http://example.com/products http://www.example.com/products https://www.example.com/products https://blog.example.com/products https://www.blog.example.com/products
but do not match:
http://www.example.com/blog/products (extra directory before the +) or http://www.example.comproducts (no trailing slash)
Once you have created the pattern that matches what you want, click the Forwarding toggle. That exposes a field where you can enter the address I want requests forwarded to.
If I enter the address above in the forwarding box and click Add Rule, within a few seconds any requests that match the pattern I entered will automatically be forwarded with a 302 Redirect to the new URL.
Advanced forwarding options
If you use a basic redirect, such as forwarding the root domain to www.example.com, then you lose anything else in the URL.
For example, you could set up the pattern:\
And have it forward to:
However, if someone entered: example.com/some-particular-page.html They would be redirected to:
The solution is to use variables. Each wildcard corresponds to a variable when can be referenced in the forwarding address. The variables are represented by a $ followed by a number. To refer to the first wildcard you’d use $1, to refer to the second wildcard you’d use $2, and so on.
To fix the forwarding from the root to www in the above example, you could use the same pattern:
You’d then set up the following URL for traffic to forward to:
In this case, if someone went to:
They’d be redirected to:
Understanding and configuring Cloudflare Page Rules (Page Rules Tutorial)
Page Rules trigger certain actions whenever a request matches one of the URL patterns you define. Learn to create and edit page rules and understand the different settings available.
In this article
You can define a page rule to trigger one or more actions whenever a certain URL pattern is matched. Page Rules are available in the Rules app, in the Page Rules tab.
The default number of allowed page rules depends on the domain plan as shown below.
|Plan||Page rules allowed|
You can purchase additional rules (up to a maximum of 100) for domains in the Free, Pro, and Business plans.
Before getting started
It is important to understand two basic Page Rules behaviors:
- Only the highest priority matching page rule takes effect on a request.
- Page rules are prioritized in descending order in the Cloudflare dashboard, with the highest priority rule at the top.
A page rule matches a URL pattern based on the following format (comprised of five segments): <scheme>://<hostname><:port>/<path>?<query_string>
An example URL with these four segments looks like:
The scheme and port segments are optional. If omitted, scheme matches both http:// and https:// protocols. If no port is specified, the rule will match all ports.
Finally, you can disable a page rule at any time. While a rule is disabled, actions won’t trigger, but the rule still appears in the Rules app in the Page Rules tab, is editable, and counts against the number of rules allowed for your domain. The Save as Draft option creates a page rule that is disabled by default.
Create a page rule
The steps to create a page rule are:
- Log in to the Cloudflare dashboard.
- Select the domain where you want to add the page rule.
- Click the Rules app.
- In the Page Rules tab, click Create Page Rule. The Create Page Rule for <your domain> dialog opens.
- Under If the URL matches, enter the URL or URL pattern that should match the rule. Learn more about wildcard matching
- Next, under Then the settings are: click + Add a Setting and select the desired setting from the dropdown. You can include more than one setting per rule. Learn more about settings in the summary below.
- In the Order dropdown, specify the desired order: First, Last or Custom.
- To save, click one of the following options:
- Save as Draft to save the rule and leave it disabled.
- Save and Deploy to save the rule and enable it immediately.
Edit a page rule
To modify an existing rule:
- Log in to the Cloudflare dashboard.
- Select the domain where you want to edit your page rule.
- Click the Rules app.
- In the Page Rules tab, locate the rule to edit.
- Proceed to make the necessary changes, as follows:
- To enable or disable a rule, click the On/Off toggle.
- To modify the URL pattern, settings, and order, click the Edit button (wrench icon). In the dialog, enter the information you’d like to change.
- To remove a rule, click the Delete button (x icon) and confirm by clicking OK in the Confirm dialog.
Understand wildcard matching and referencing
You can use the asterisk (*) in any URL segment to match certain patterns. For example,
example.com/test example.com/toast example.com/trust
example.com/foo/* does not match example.com/foo. However, example.com/foo* does.
- To match both http and https, just write example.com. It is not necessary to write *example.com.
- To match every page on a domain, write example.com/*. Just writing example.com won’t work.
- To match every page on a domain and it’s subdomains, write *example.com/*. Just writing example.com won’t work.
- A wildcard (*) in a Page Rule URL will match even if no characters are present, and may include any part of the URL, including the query string.
Referencing wildcard matches
You can reference a matched wildcard later using the $X syntax. X indicates the index of a glob pattern. As such, $1 represents the first wildcard match, $2 the second wildcard match, and so on.
This is specifically useful with the Forwarding URL setting. For example:
You could forward:
This rule would match:
which ends up being forwarded to:
To use a literal $ character in the forwarding URL, escape it by adding a backslash (\) in front: \$.
Summary of Page Rules Settings
Settings control the action Cloudflare takes once a request matches the URL pattern defined in a page rule. You can use settings to enable and disable multiple Cloudflare features across several of the dashboard apps. Note that:
- Some settings require a Pro, Business or Enterprise domain plan.
- You can specify more than one setting to apply when the rule triggers.
- Ports 80, 443, and 8080 are the only ports where Cloudflare Caching is available.
Below is the full list of settings available, presented in the order that they appear in the Cloudflare Page Rules UI.
|Always Online||Turn on or off the Always Online feature of the Cloudflare Caching app. Learn more.
Disable this for sections of your site that should never return cached data, such as APIs or payment/cart pages.
|Always Use HTTPS||Turn on or off the Always Use HTTPS feature of the Edge Certificates tab in the Cloudflare SSL/TLS app. If enabled, any http:// URL is converted to https:// through a 301 redirect.
If this option does not appear, you do not have an active Edge Certificate.
|Auto Minify||Indicate which file extensions to minify automatically. Learn more.||
|Automatic HTTPS Rewrites||Turn on or off the Cloudflare Automatic HTTPS Rewrites feature of the Edge Certificates tab in Cloudflare SSL/TLS app. Learn more.||
|Browser Cache TTL||Control how long resources cached by client browsers remain valid. The Cloudflare UI and API both prohibit setting Browser Cache TTL to 0 for non-Enterprise domains. Learn more.||
|Browser Integrity Check||Inspect the visitor’s browser for headers commonly associated with spammers and certain bots. Learn more.||
|Bypass Cache on Cookie||Bypass Cache and fetch resources from the origin server if a regular expression matches against a cookie name present in the request.
If you add both this setting and the Cache On Cookie setting to the same page rule, Cache On Cookie takes precedence over Bypass Cache on Cookie.
See Additional details below to learn about limited regular expression support.
|Cache By Device Type||Separate cached content based on the visitor’s device type. Learn more.||
|Cache Deception Armor||Protect from web cache deception attacks while still allowing static assets to be cached. This setting verifies that the URL’s extension matches the returned Content-Type. Learn more.||
|Cache Key||Also referred to as Custom Cache Key.
Control specifically what variables to include when deciding which resources to cache. This allows customers to determine what to cache based on something other than just the URL. Learn more.
|Cache Level||Apply custom caching based on the option selected:
Bypass – Cloudflare does not cache.
No Query String – Delivers resources from cache when there is no query string.
Ignore Query String – Delivers the same resource to everyone independent of the query string.
Standard – Caches all static content that has a query string.
Cache Everything – Treats all content as static and caches all file types beyond the Cloudflare default cached content. Respects cache headers from the origin web server unless Edge Cache TTL is also set in the Page Rule. When combined with an Edge Cache TTL > 0, Cache Everything removes cookies from the origin web server response.
|Cache on Cookie||Apply the Cache Everything option (Cache Level setting) based on a regular expression match against a cookie name.
If you add both this setting and Bypass Cache on Cookie to the same page rule, Cache On Cookie takes precedence over Bypass Cache on Cookie.
|Disable Apps||Turn off all active Cloudflare Apps.||
|Disable Performance||Turn off:||
|Disable Railgun||Turn off the Railgun feature of the Cloudflare Speed app.||
|Disable Security||Turn off:||
|Edge Cache TTL||Specify how long to cache a resource in the Cloudflare edge network. Edge Cache TTL isn’t visible in response headers. The minimum Edge Cache TTL depends on plan type:
Free – 2 hours
|Email Obfuscation||Turn on or off the Cloudflare Email Obfuscation feature of the Cloudflare Scrape Shield app. Learn more.||
|Forwarding URL||Redirects one URL to another using an HTTP 301/302 redirect. See Understand wildcard matching and referencing above.||
|Host Header Override||Apply a specific host header. Learn more.||
|IP Geolocation Header||Cloudflare adds a CF-IPCountry HTTP header containing the country code that corresponds to the visitor.||
|Mirage||Turn on or off Cloudflare Mirage of the Cloudflare Speed app. Learn more.||
|Opportunistic Encryption||Turn on or off the Cloudflare Opportunistic Encryption feature of the Edge Certificates tab in the Cloudflare SSL/TLS app. Learn more.||
|Origin Cache Control||Origin Cache Control is enabled by default for Free, Pro, and Business domains and disabled by default for Enterprise domains.||
|Origin Error Page Pass-thru||Turn on or off Cloudflare error pages generated from issues sent from the origin server. If enabled, this setting triggers error pages issued by the origin.||
|Polish||Apply options from the Polish feature of the Cloudflare Speed app. Learn more.||
|Query String Sort||Turn on or off the reordering of query strings. When query strings have the same structure, caching improves. Learn more.||
|Resolve Override||Change the origin address to the value specified in this setting. Learn more.||
|Respect Strong ETags||Turn on or off byte-for-byte equivalency checks between the Cloudflare cache and the origin server. Learn more.||
|Response Buffering||Turn on or off whether Cloudflare should wait for an entire file from the origin server before forwarding it to the site visitor. By default, Cloudflare sends packets to the client as they arrive from the origin server.||
|Rocket Loader||Turn on or off Cloudflare Rocket Loader in the Cloudflare Speed app. Learn more.||
|Security Level||Control options for the Security Level feature from the Cloudflare Firewall app. Learn more.||
|Server Side Excludes||Turn on or off the Server Side Excludes feature of the Cloudflare Scrape Shield app. Learn more.||
|SSL||Control options for the SSL feature of the Edge Certificates tab in the Cloudflare SSL/TLS app. Learn more.||
|True Client IP Header||Turn on or off the True-Client-IP Header feature of the Cloudflare Network app. Learn more.||
|(legacy) Web Application Firewall||Turn on or off your (legacy) Web Application Firewall rules as defined in the Cloudflare Firewall app. Learn more. Learn more.
Individual WAF rules cannot be enabled or disabled via page rules.
Page Rule configuration issue leading to “Error 500 (Internal server error)“
Root cause: This may be due to a configuration issue on a Page Rule. When creating a Page Rule that uses two wildcards, like a Forwarding URL rule, it is possible to create a rule that mentions the second wildcard with the $2 placeholder. See example below:
When updating the same rule, you can remove one of the wildcard in the If the URL matches field and save it. See example below:
If you do so, the $2 placeholder reference a wildcard that does not exist anymore, and as such, an “Error 500 (Internal server error)” is thrown when a URL triggers the page rule.
Resolution: Update the Page Rule and remove the reference $2 to the second wildcard. If there is only one wildcard, then only $1 can be used.
Bypass Cache on Cookie setting
This setting is available to business and enterprise customers.
The Bypass Cache on Cookie setting supports basic regular expressions (regex) as follows:
- A pipe operator (represented by |) to match multiple cookies using OR boolean logic. For example, bypass=.*|PHPSESSID=.* would bypass the cache if either a cookie called bypass or PHPSESSID were set, regardless of the cookie’s value.
- The wildcard operator (represented by .*), such that a rule value of “t.*st=” would match both a cookie called test and one called teeest.
- 150 chars per cookie regex
- 12 wildcards per cookie regex
- 1 wildcard in between each | in the cookie regex
To learn how to configure Bypass Cache on Cookie with a variety of platforms, review these articles:
- Caching Anonymous Page Views with WordPress or WooCommerce
- Caching Anonymous Page Views with Magento 1 and Magento 2
- How do I cache static HTML?
Note: If you add both this setting and the enterprise-only Cache On Cookie setting to the same page rule, Cache On Cookie takes precedence over Bypass Cache on Cookie.
Zone name occurrences must end with a slash
When saving a Page Rule, Cloudflare will ensure that there is a slash after each occurrence of the current zone name in the If the URL matches field. For example, if the current zone name is
example.comwill be saved as
example.com/path/example.comwill be saved as
example.com/some-path/cloudflare.com will be saved without a final slash, since the zone name is not
Workers custom routes take precedence over Page Rules
If the URL of the current request matches both a Page Rule and a Workers custom route, the Worker script assigned to the custom route will execute and the Page Rule will not be applied.