Create Edge Cache TTL page rules
Get Started
Before you begin, you should understand two basic page rule 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.
Cloudflare recommends ordering your rules from most specific to least specific.
Page Rules Explained
A page rule matches a URL pattern based on the following format (comprised of five segments): <scheme>://<hostname><:port>/<path>?<query_string>
A URL with five segments look similar to the example below.
https://www.example.com:443/image.png?parameter1=value1
The scheme
and port
segments are optional. If omitted, scheme matches both http://
and https:// protocols
. If no port is specified, the rule matches all ports.
You can disable a page rule at any time. While a rule is disabled, actions will not trigger, but the rule still appears in the Rules > 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.
Page rules trigger certain actions when a request matches a defined URL pattern.
Plan | Page rules allowed |
---|---|
Free | 3 |
Pro | 20 |
Business | 50 |
Enterprise | 125 |
You can also purchase additional rules up to a maximum of 100 for domains in the Free, Pro, and Business plans.
Create a page rule
- Log in to your Cloudflare dashboard.
- Select the domain where you want to add the page rule.
- Select Rules.
- In the Page Rules tab, select 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
- Under Then the settings are, select Add a Setting.
- Select an option from the dropdown. You can include more than one setting per rule. Learn more about settings in the summary below.
- From the Order dropdown, specify the desired order: First, Last, or Custom.
- Choose a save option:
- Save as Draft to save the rule and leave it disabled. Note that disabled rules count towards the number of rules allowed for your domain.
- Save and Deploy to save the rule and enable it immediately.
Edit existing page rules
- Log in to your Cloudflare dashboard.
- Select the domain where you want to edit your page rule.
- Select Rules.
- In the Page Rules tab, locate the rule to edit.
- Proceed to make the necessary changes, as follows:
- To enable or disable a rule, switch the toggle On/Off.
- To modify the URL pattern, settings, or order, select the Edit button (wrench icon). In the dialog, enter the information you want to change.
Delete a rule
- Log in to your Cloudflare dashboard.
- Select the domain where you want to edit your page rule.
- Select Rules.
- In the Page Rules tab, locate the rule to edit.
- Select the Delete button (wrench icon) and confirm the deletion when prompted.
Cache by device type (Enterprise only)
Enterprise domains can cache content by device type to target visitors with content appropriate to their device. Cloudflare evaluates the User-Agent header in the HTTP request to identify the device type and identifies each device type with a case insensitive match to the regex below:
- Mobile:
(?:phone|windows\s+phone|ipod|blackberry|(?:android|bb\d+|meego|silk|googlebot) .+? mobile|palm|windows\s+ce|opera\ mini|avantgo|mobilesafari|docomo|KAIOS)
- Tablet:
(?:ipad|playbook|(?:android|bb\d+|meego|silk)(?! .+? mobile))
- Desktop: Everything else not matched above.
- Log in to your Cloudflare account.
- Select the appropriate domain.
- Select Rules > Page Rules.
- Select Create Page Rule.
- Enter the URL to cache by device type.
- Choose the Cache By Device Type setting.
- Select On.
- Select Save and Deploy.
Once enabled, Cloudflare sends a CF-Device-Type
HTTP header to your origin with a value of either mobile
, tablet
, or desktop
for every request to specify the visitor’s device type. If your origin responds with the appropriate content for that device type, Cloudflare only caches the resource for that specific device type.
To purge resources using Cache By Device Type, you must purge by Cache-Tag.
Cache Everything
Caching additional content at Cloudflare requires a Cache Everything Page Rule. Without creating a Cache Everything Page Rule, dynamic assets are never cached even if a public Cache-Control header is returned. When combined with an Edge Cache TTL > 0, Cache Everything removes cookies from the origin web server response.
- Log in to your Cloudflare account.
- Choose the appropriate domain.
- Select Rules > Page Rules.
- Select Create Page Rule.
- Under If the URL matches, create a URL pattern to differentiate your website’s static versus dynamic content.
- Under Then the settings are, choose Cache Level.
- For Select Cache Level, choose the Cache Everything submenu setting.
- Select Save and Deploy.
- Verify your resources are cached by checking the cache response returned by Cloudflare.
Cloudflare caches XML responses when using Cache Everything. By default, jquery’s getScript explicitly bypasses cache by appending a timestamp query string unless the behavior is disabled. For further details on how to adjust Cloudflare’s behavior for query strings, refer to our article on Caching Levels.