1. Home
  2. Knowledge Base
  3. SEO Suite
  4. How To Exclude Rank Math Sitemaps from Caching

How To Exclude Rank Math Sitemaps from Caching

Caching is one of the easiest and most effective ways to speed up your website. In WordPress, using 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, the sitemap of your website ends up being cached as well, 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 work for other plugins as well.

Let’s begin.

1 htaccess

Before making any changes to your cache plugin’s settings, adding a rewrite rule to your .htaccess file for controlling the cache can resolve the issue on most occasions. Your website’s .htaccess file can be edited directly inside your WordPress dashboard by using Rank Math, and to edit .htaccess, navigate to Rank Math > General Settings > Edit .htaccess inside your WordPress admin area.

Edit .htaccess file with Rank Math

Editing your site’s .htaccess file can, at times, break your site if you accidentally make any mistakes. Hence, Rank Math gives you a warning and keeps the text area disabled on this page to avoid any accidental edits. You can add the following code to the text area once you click the checkbox below the warning.

RewriteEngine On
RewriteRule .* - [E=Cache-Control:no-cache]

Once you’re making the changes, click the Save Changes button.

add code

Now, check your sitemap to see if it is excluded from the cache, if not, proceed with the next step to change your cache plugin’s configuration.

2 W3 Total Cache

To exclude Rank Math’s sitemap from being cached by W3 Total Cache, head over to Performance > Page Cache. There, enter the following code in the section which says, “Never cache the following pages“.


It is a simple regular expression that tells W3 Total Cache to exclude XML and XSL files with “sitemap” in the name.

Exclude Sitemap In W3 Total Cache

Since W3 Total Cache supports multiple types of caching, we have to make sure that none of those caching methods cache the sitemap. Here is what you need to do for the different cache types inside W3 Total Cache.

2.1 Database Caching

If you’re using database caching, head over to Performance > Database Caching > Never Cache the Following Pages and add the same strings there as well.

Exclude Sitemap In Database Cache W3 Total Cache

2.2 Minification

If you’re using minification with W3 Total Cache, head over to Performance > Minify > Never minify the following Pages and add the same strings there as well.

Exclude Sitemap In Minify W3 Total Cache

2.3 404 Error Exception List

W3 Total Cache also caches 404 errors on your website, which can lead to caching of the sitemap. To prevent this, head over to Performance > Browser Cache and add the same lines to the 404 error exception list.

Important Note: Recent versions of W3 Total Cache already have this exception added to the code, and you might see something similar on your website if you’re using the latest version.

Exclude Sitemap In Minify W3 Total Cache

If you see this, the sitemap will be excluded already, and you don’t have to paste in the code snippet here.

3 LiteSpeed Cache

To exclude your sitemap being cached by LiteSpeed Cache, head over to LiteSpeed Cache > Cache > Excludes and enter the following code in the Do Not Cache URIs field.


This will prevent and XML or XSL files from being cached.

Exclude Sitemap Caching In LiteSpeed Cache

4 WP Rocket

To exclude your sitemap from being cached by WP Rocket, head over to Settings > WP Rocket > Advanced Rules> Never Cache URL(s). Inside, enter the following code snippet and save your settings.


5 WP Super Cache

To exclude your sitemap from being cached by WP Super Cache, head over to WP Super Cache’s settings, then go to the Advanced Tab.

Head To Advanced Settings In WP Super Cache

Scroll way down to find the section which says, “Add here strings (not a filename) that force a page not to be cached”. The section is not highlighted, so look carefully. Once you find it, add the following code snippet inside.


Here is how it should appear for you once the code snippet is added.

Add Code Snippet To WP Super Cache

6 WP Fastest Cache

To exclude your sitemap from WP Fastest Cache, go to WP Fastest Cache > Exclude. In the exclude section, click the Add New Rule button in the Exclude Pages section.

Add New Rule In Exclude Tab In WP Fastest Cache

In the popup that appears, choose the Contains option in the first dropdown, then type in sitemap in the text field next to it. Finally, save your settings.

Configure Sitemap Exclusion In WP Fastest Cache

Now WP Fastest Cache won’t cache your sitemaps.

7 Cloudflare

Many people also use Cloudflare on top of caching solutions to speed up websites, and it would be best if the sitemap was not cached by Cloudflare. The good news is that Cloudflare does not cache sitemaps by default, so you won’t have to do anything. However, if you’ve made some changes in the settings that are causing sitemaps to be cached, you can set a page rule inside your Cloudflare account and add the following code snippet there.


Here is a support thread confirming this. Also, here is a guide by Cloudflare on how to use their page rules.

8 Hummingbird Cache

To exclude your sitemap from Hummingbird Cache, navigate to Hummingbird Settings > Caching. Then, in the Caching section, select the Page Caching tab and click on Activate.

Page Caching

Then navigate to the Exclusions option. There should already be an entry for sitemap\.xml, but if it is not there, or the file is named differently, you should add the name here as well.

Exclusions in Hummingbird Cache

Finally, save your settings once you have made the changes.

9 Transient Cache

Sometimes the sitemap can be affected by the server-level cache. You should disable transient caching to prevent the sitemap from going blank or empty. In such a case, you can add the following code to your theme’s functions.php file or rank-math.php file to make sure your sitemap is excluded from caching:

 * 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 any other caching plugins, then you’ll have to do some searching to find the solution. Most caching plugins do offer this functionality, so you wouldn’t have much trouble. Also, the code snippet we shared is regex code, so any plugin supporting regex should be able to accept the code. If you can’t find solutions, you can always contact the plugin developer or your hosting provider for support.

Next Steps

No matter what caching plugin you’re using, make sure to save your changes and empty your cache after you’ve made these changes. Make sure to double-check if your settings were saved as many times you might navigate away from the page without explicitly saving your settings.

Was this article helpful?

Still not using Rank Math?

Setup takes less than 5 minutes including the import from your old SEO Plugin!

Learn more about the PRO Version

Still need help?


Submit Your Question

Please give us the details, our support team will get back to you.

Open Ticket

Related Articles