Caching is one of the easiest and most effective ways to speed up your website. In WordPress, caching is as simple as installing a caching plugin and configuring some basic settings.
However, this usually leads to a problem. When caching is enabled, your website’s sitemap may also be cached, which is the opposite of what should happen. Since a sitemap should always be up to date with the pages, posts, and custom posts on your website, caching it defeats the purpose.
That is why we’ll show you how to exclude the sitemap generated by Rank Math from being cached. We will cover the most popular caching plugins, but the methods should also work for other plugins.
Let’s begin.
Jump To:
1 htaccess
Before making any changes to your cache plugin’s settings, you should add a rewrite rule to your .htaccess file and confirm if it resolves the issue. To do that, navigate to Rank Math SEO → General Settings → Edit .htaccess from your WordPress dashboard.Â
Editing your site’s .htaccess file can break your site if you make any mistakes. Hence, Rank Math displays a warning and keeps the text area disabled to avoid any accidental edits.
You can add the below code to the text area once you click the checkbox below the warning. Once done, click Save Changes.
Note: You can use the htaccess tester tool to debug and ensure the rewrites are correct before adding them to the .htaccess file.
RewriteEngine On
RewriteRule .* - [E=Cache-Control:no-cache]
The next step is to confirm whether your sitemap is excluded from the cache. If not, proceed with the below steps to change your cache plugin’s configuration.
2 WP Rocket
To exclude your sitemap from being cached by WP Rocket, head over to Settings → WP Rocket → Advanced Rules → Never Cache URL(s). Once done, enter the below code snippet and save your settings.
/(.*)sitemap(.*).xml
/(.*)sitemap.xsl
3 W3 Total Cache
To exclude Rank Math’s sitemap from being cached by W3 Total Cache, head over to Performance → Page Cache from your WordPress dashboard. Then, enter the below code in the Never cache the following pages settings.  Once done, click Save all settings to save your changes.
sitemap(_index)?\.xml(\.gz)?
[a-z0-9_\-]*sitemap[a-z0-9_\-]*\.(xml|xsl|html)(\.gz)?
([a-z0-9_\-]*)?\.xml
The above code is a simple regular expression (regex) that tells W3 Total Cache to exclude all XML and XSL files with “sitemap” in the name from being cached.
Since W3 Total Cache supports multiple caching methods, you should ensure that none caches the sitemap. Here is how to do that for the different cache types inside W3 Total Cache.
3.1 Database Caching
If you use database caching, head over to Performance → Database Cache. Then, scroll to the Never cache the following pages option and add the code snippet, as shown below. Once done, click Save all settings.Â
3.2 Minification
If you use minification in W3 Total Cache, navigate to Performance → Minify. Then, scroll down to the Never minify the following pages option and paste the code snippet, as shown below. Once done, click Save all settings.
3.3 404 Error Exception List
W3 Total Cache also caches 404 errors on your website, which can lead to the sitemap getting cached. To prevent this, head over to Performance → Browser Cache. Then, add the code snippet to the 404 error exception list settings, as shown below. Once done, click Save all settings.
Note: Recent versions of W3 Total Cache have this code snippet added to the 404 error exception list by default. In that case, you don’t need to add another code snippet.
4 LiteSpeed Cache
To exclude your sitemap from being cached by LiteSpeed Cache, head over to LiteSpeed Cache → Cache → Excludes from your WordPress dashboard. Once done, paste the below code snippet into the Do Not Cache URIs field and click Save Changes.
/(.*)sitemap(.*).xml
/(.*)sitemap.xsl
/sitemap_index.xsl
/sitemap_index.xml
5 WP Super Cache
To exclude your sitemap from being cached by WP Super Cache, head over to Settings → WP Super Cache → Advanced from your WordPress dashboard.
Once done, navigate to Rejected URL Strings and paste the below code snippet. Then, click Save Strings.
sitemap?\.xml?
[a-z0-9_\-]*sitemap[a-z0-9_\-]*.(\xml|xsl)?
([az0-9_\-]*?)sitemap([a-z0-9_\-]*)?.xml
Here is how it should appear for you once the code snippet is added.
6 WP Fastest Cache
To exclude your sitemap from WP Fastest Cache, navigate to WP Fastest Cache → Exclude from your WordPress dashboard. In the Exclude Pages settings, click Add New Rule, as shown below.
In the Exclude Page Wizard popup, select Contains in the dropdown menu and type sitemap into the text field. Once done, click Save, as shown below.
7 Cloudflare
Cloudflare does not cache sitemaps by default. However, if you’ve made some changes that cause your sitemaps to be cached, you can set a page rule to exclude your sitemap from being cached.
To do that, log into your Cloudflare account and select your website. Then, navigate to Rules → Page Rules and click Create Page Rule, as shown below.
In the Page Rules settings, paste your sitemap URL in the URL field. Then, select Cache Level and Bypass in the dropdown menus. Once done, click Save and Deploy Page Rule.
Note: If you navigate to Rank Math SEO → Sitemap Settings from your WordPress dashboard, your sitemap URL will be displayed at the top of the General settings page.Â
8 Hummingbird Cache
To exclude your sitemap from Hummingbird Cache, navigate to Hummingbird → Caching → Page Caching from your WordPress dashboard.Â
In the Page Caching settings, navigate to Exclusions and paste the below code snippet. Once done, click SAVE SETTINGS.
sitemap[^\/.]*\.xml
9 Transient Cache
Sometimes the sitemap can be affected by a server-level cache. In such a case, you can add the below filter to your theme’s functions.php file or rank-math.php file to exclude your sitemap from being cached. You can refer to this guide on adding filters to a WordPress site.
/**
* Filter if XML sitemap transient cache is enabled.
*
* @param boolean $unsigned Enable cache or not, defaults to true
*/
add_filter( 'rank_math/sitemap/enable_caching', '__return_false');
10 Other Caching Plugins
If you’re using other caching plugins, you’ll have to do some research to find a solution. Most caching plugins allow you to disable your sitemap from being cached, so you wouldn’t have much trouble finding a solution.
Also, the code snippets we shared are regular expression (regex) codes, so any plugin that supports regex will accept the code. If you can’t find a solution, contact the plugin developer or your hosting provider for support.
Next Steps
No matter what caching plugin you use, ensure to save your changes and empty your cache after you’ve made these changes. Make sure to double-check your settings were saved, as you might navigate away from the page without saving your settings.