How To Setup Cloudflare Cache Rules for WordPress

By default, Cloudflare only allows Free users to setup 3 Page Rules. These page rules are what allows WordPress users to differentiate which pages/files etc. should be cached and served from Cloudflare servers and which should be served from the original web hosting.

My old Page rule setup is as shown below. Number 2 prevents my wordpress admin dashboard from being served via Cloudflare while every other part of the website is served via Cloudflare.

Cloudflare WordPress Page Rules

 

With Page Rules, there’s a limit to how many URLs you can setup, but this isn’t so with Cache Rules. With just 2 of the 10 Cache Rules offered for free, you can setup a lot of URLs as I’ll show soon.

Here’s what my Cache Rules page looks like.

 

Cloudflare WordPress Cache Rules

 

The first one Cache Everything is targeted towards caching everything on the site on Cloudflare servers. Simply click on Create Rule to begin then replicate everything in the screenshot below whilst replacing domain name with yours.

When you’re done, click on Deploy and it will go live.

 

Cache Everything

 

My second cache rule is targeted towards excluding special parts of my WordPress installation from being cached by Cloudflare. This includes WP Admin, WP Login, Sitemaps, Search Pages, WordPress Cookies, URLs etc.

Below is what my current setup looks like. You can click to enlarge.

 

Web capture_26-10-2023_12516_dash.cloudflare.com

If you wish to not edit each line one by one, simply proceed as follows. Create a new Cache Rule, give it a name and then click on Edit Expression

Create New Cloudflare Cache Rule

Copy and Paste the following rules below into the field.

(http.request.full_uri contains “/wp-admin”) or (http.request.full_uri contains “/wp-login.php”) or (http.request.full_uri contains “https://www.YOURDOMAIN.com/?s=”) or (http.request.full_uri contains “/checkout/”) or (http.request.full_uri contains “/cart/”) or (http.request.full_uri contains “/my-account/”) or (starts_with(http.request.full_uri, “/graphql”)) or (starts_with(http.request.full_uri, “/xmlrpc.php”)) or (http.cookie contains “no_cache”) or (http.cookie contains “wp-“) or (http.cookie contains “wordpress-“) or (http.cookie contains “comment_”) or (http.cookie contains “woocommerce_”) or (http.cookie contains “PHPSESSID”)

Don’t forget to replace YOURDOMAIN with yours

Under Cache Eligibility select Bypass Cache then save as draft or deploy.

 

Screenshot 2023-10-26 114801

 

If you wish to exclude more URLs, simple click on OR at the end of the last added URL. Select URI Full then Equals and enter the full URL and save.

That’s all.

Deploy the two rules you’ve created and make sure to arrange them is such a way that Cache Everything is the first listed Cache Rule.

 

Cloudflare WordPress Cache Rules

If you’ve got questions, hit me up in the comment section.

H/T: Guide to HTML Full Page Caching with Cloudflare for WordPress

Reviewer's Rating!
Leave Your Own Rating!
[Total: 0 Average: 0]

You’ll also like:

  1. How To Fix Converter For Media Not Working With Cloudflare
  2. FIXED! How To Edit WP-Config While Using Cloudflare
  3. How To Setup Hyper Cache On Shared Hosting Server
  4. How To Use Custom Theme With Jetpack Mobile Theme Feature
  5. Fixed: Eliminate render-blocking JavaScript and CSS In Above-The-Fold content

Please share this article to help others. Thanks