Create a rule in the dashboard
-
In the Cloudflare dashboard, go to Cache > Cache Rules.
Go to Cache Rules -
Select the Cache Response Rules tab.
-
Select Create rule.
-
Enter a descriptive name for the rule in Rule name.
-
Under When incoming requests match, select All incoming requests if you want the rule to apply to all traffic or Custom filter expression if you want the rule to only apply to traffic matching the custom expression.
-
If you selected Custom filter expression, define the rule expression. Use the Field drop-down list to choose an HTTP property and select an Operator. Both request fields (such as URI path or hostname) and response fields (such as response status code or response headers) are available for matching. Refer to Available settings for the full list of available fields and operators.
-
Following the selection of the field and operator, enter the corresponding value that will trigger the Cache Response Rule. For example, if the selected field is
Hostnameand the operator isequals, a value ofexample.comwould mean the rule matches any request to that hostname. -
Under Then, select one of the following actions:
-
Modify cache-control directives: Set or remove
Cache-Controldirectives sent by your origin. For each directive, choose Set directive or Remove directive. For duration-based directives likemax-ageors-maxage, enter a value in seconds. Turn on Cloudflare only to apply the directive only within Cloudflare's cache without changing what visitors receive. Refer to Supported directives for the full list. -
Modify cache tags: Add, override, or remove cache tags on the response for targeted purging. Select one of the following operations:
- Add to existing tags: Append new tags to the current set.
- Override existing tags: Replace all current tags with the specified tags.
- Remove from existing tags: Remove specific tags from the current set.
For the tag source, you can either specify tags manually or select Parse from response header to extract tags from a response header value. When parsing from a header, you can split the header value using a custom separator (for example, commas instead of spaces).
-
Strip headers: Remove
Set-Cookie,ETag, orLast-Modifiedheaders from the origin response before Cloudflare evaluates the response for caching. Select which headers to strip.
For more details on each action, refer to Available settings.
-
-
Under Place at, from the dropdown, you can select the order of your rule. From the main page, you can also change the order of the rules you have created.
-
To save and deploy your rule, select Deploy. If you are not ready to deploy your rule, select Save as Draft.
If you are matching a hostname in your rule expression, you may be prompted to create a proxied DNS record for that hostname. Refer to Troubleshooting for more information.