How to Exclude XML Sitemaps from Caching in WordPress

Using caching plugins is probably the easiest way to to speed up your WordPress site. However, you might end up getting your sitemap cached too and that’s not a good idea.

The XML sitemap should always reflect the latest posts and urls of your website so search engines get the latest version of whatever’s updated.

In this post, I’d like to share with you how to exclude and prevent XML sitemaps from being cached (unintentionally) by some of the most popular WordPress Caching plugins.

W3 Total Cache

In order to exclude the XML Sitemap from caching using W3 Total Cache plugin, here’s what you do:

  1. Go to Performance > Page Cache.
  2. w3 total
  3. Scroll down to the section labeled "Never cache the following pages". Then, enter the following lines in the box:
  4. [a-z0-9_\-]*sitemap[a-z0-9_\-]*\.(xml|xsl|html)(\.gz)?

If you are using Minification, open Performance > Minify and enter the same lines in the field saying "Never minify the following pages".

Last but not least, go to Performance > Browser Cache and ensure the same lines are in the "404 error exception list" section.

WP Super Cache

In order to exclude the XML sitemap from caching with WP Super Cache plugin, here what you do:

  1. Go to Settings > WP Super Cache. Open Advanced tab.
  2. wpsupercache options
  3. Look for the section labeled "Add here strings (not a filename) that forces a page not to be cached..." and enter the following:
  4. [a-z0-9_\-]*sitemap[a-z0-9_\-]*\.(xml|xsl|html)(\.gz)?

WP Rocket

If you use the WP Rocket plugin and you want to exclude the XML sitemap from caching, here’s what you do:

  1. Go to Settings > WP Rocket > Advanced Options.
  2. wp-rocket

  3. Scroll down to the block with the words: "Never cache the following pages". Enter these lines in the field:
  4. [a-z0-9_\-]*sitemap[a-z0-9_\-]*\.(xml|xsl|html)(\.gz)?