Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save luckyankit/2685f7c16419fc4739d00937ff73c61a to your computer and use it in GitHub Desktop.
Save luckyankit/2685f7c16419fc4739d00937ff73c61a to your computer and use it in GitHub Desktop.
Super Page Cache for Cloudflare — Guide for using Remove Cache Buster Query Parameter feature (when using Cache Everything page rule)

Implementation Guide for using "Remove Cache Buster Query Parameter" feature

The Super Page Cache for Cloudflare plugin has recently added the feature for using the Cache Everything pagerule withing the ?swcfpc=1 cache buster query paramater. This opens up so many new doors where users previously had to use the Cloudflare Workers to remove the cache buster.

With this new option now users are able to take advantage of Cloudflare Cache Everything page rule and take it to the next level by using the new Rulesets released by Cloudflare. Basically this is achived by taking advantage of the all new Cache Rules feature implemented by Cloudflare.


Setp 1 — Setting up the Cache Rules inside your Cloudflare Dashboard

The first thing that you need to do is, log-in to your Cloudflare Dahsbord and go to the domain/zone doe which you are setting up the Super Page Cache for Cloudflare plugin. Then from the left hand side menus, go to Caching > Cache Rules.

Cache Rules Section

Then inside the Cache Rules page, click on the Create Cache Rule button. Then finally on the Create new Cache Rule page, click on the Edit Expression link.

Edit Expression Link

Now it's time to create all the Cache Rules we need... So, let's get started...

Super Important Note Badge
Make sure you replace example.com with your actual website hostname e.g. something.com or www.something.com etc. in the code given below before copy/pasting it.

Rule 1 ➜ Cache Bypass — WP Admin Paths, WooCommerce API, EDD API Endpoints

Rule Name: Add the following in the Rule Name section Cache Bypass — WP Admin Paths, WooCommerce API, EDD API Endpoints

Rule Expression: Add the following expression inside the expression builder section.

(
  http.host eq "example.com" and
  (starts_with(http.request.uri.path, "/wp-admin") or starts_with(http.request.uri.path, "/wc-api/") or starts_with(http.request.uri.path, "/edd-api/"))
)

Cache Status: Set it to Bypass Cache.

Rule 2 ➜ Cache Bypass — XML, XSL & PHP Files

Rule Name: Add the following in the Rule Name section Cache Bypass — XML, XSL & PHP Files

Rule Expression: Add the following expression inside the expression builder section.

(
  http.host eq "example.com" and
  (http.request.uri.path contains ".xsl" or http.request.uri.path contains ".xml" or http.request.uri.path contains ".php")
)

Cache Status: Set it to Bypass Cache.

Rule 3 ➜ Cache Bypass — Default Bypass Cookies

Rule name: Add the following in the Rule Name section Cache Bypass — Default Bypass Cookies

Rule Expression: Add the following expression inside the expression builder section.

(
  http.host eq "example.com" and
  (http.cookie contains "wordpress_logged_in_" or http.cookie contains "comment_" or http.cookie contains "woocommerce_" or http.cookie contains "wordpressuser_" or http.cookie contains "wordpresspass_" or http.cookie contains "wordpress_sec_" or http.cookie contains "yith_wcwl_products" or http.cookie contains "edd_items_in_cart" or http.cookie contains "it_exchange_session_" or http.cookie contains "comment_author" or http.cookie contains "dshack_level" or http.cookie contains "auth_" or http.cookie contains "noaffiliate_" or http.cookie contains "mp_session" or http.cookie contains "xf_" or http.cookie contains "mp_globalcart_") and
  not http.request.uri.path contains "."
)

Cache Status: Set it to Bypass Cache.


Optional Rule

Rule 4 ➜ Cache Eligible Requests & Ignore Query Params from cacheKey

Rule name: Add the following in the Rule Name section Cache Eligible Requests & Ignore Query Params from cacheKey

Rule Expression: Add the following expression inside the expression builder section.

(http.host eq "example.com" and not starts_with(http.request.uri.path, "/wp-admin") and not starts_with(http.request.uri.path, "/wp-json/") and not starts_with(http.request.uri.path, "/wc-api/") and not starts_with(http.request.uri.path, "/edd-api/") and not http.request.uri.path contains ".xsl" and not http.request.uri.path contains ".xml" and not http.request.uri.path contains ".php" and not http.cookie contains "wordpress_logged_in_" and http.cookie ne "comment_" and not http.cookie contains "woocommerce_" and not http.cookie contains "wordpressuser_" and not http.cookie contains "wordpresspass_" and not http.cookie contains "wordpress_sec_" and not http.cookie contains "yith_wcwl_products" and not http.cookie contains "edd_items_in_cart" and not http.cookie contains "it_exchange_session_" and not http.cookie contains "comment_author" and not http.cookie contains "dshack_level" and not http.cookie contains "auth_" and not http.cookie contains "noaffiliate_" and not http.cookie contains "mp_session" and not http.cookie contains "xf_" and not http.cookie contains "mp_globalcart_")

Cache Status: Set it to Eligible for cache.

Cache Key:

In this section enable the following options:

  • Cache deception armor
  • Ignore query string
  • Ignore query string order

Finally the cache rule should look something like this ☟ Cache Eligible Requests & Ignore Query Params from cacheKey Rule Screenshot

Very Important Note Badge
Once you add this fourth cache rule, Cloudflare will simply ignore the query strings when checking the cached contents. So, if you enter https://example.com/some-page/?fbcid=123&foo=bar&something=test, Cloudflare will simply ignote the ?fbcid=123&foo=bar&something=test part and check if https://example.com/some-page/ is cached, if so, return the cached content.
In most websites, this will not cause any issues due to the rules we have created above, but still if your website is dependend on query params, after implementing this rule, please test your website thoroughly to ensure everything is working as expected.

Once you have all these Cache Rule added to your Cloudflare dashboard, you can proceed to the Step 2 below.

Step 2 — Enabling Remove Cache Buster Query Parameter option in the plugin settings & Purge the whole Cloudflare Cache

Inside the Super Page Cache for Cloudflare plugin settings, go to the Other tab and scroll down. You will see an option named Remove Cache Buster Query Parameter.

plugin settings to enable

Enable that option and save the plugin settings. Then all you need to do is to make sure that you have force purged everything from the Cloudflare Cache.

That's it. Now you have the Cloudflare CDN Level Page Caching with the Cache Everything Page Rule without any cache buster query parameter (e.g. ?swcfpc=1). Also if you have added the fouth Cache Rule then your cache HIT ratio will also increase dramatically as Cloudflare will now ignore the query strings when checking if the URL is already cached. Enjoy... 🥂🍾🥳🎉

P.S.: Make sure you have enabled Smart Tiered Cache inside your Cloudflare Dashboard for the highest cache HIT ratio.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment