Prevent WP Rocket from Caching Filtered WooCommerce pages

When using WP Rocket with preloading in combination with a filter plugin like Filter Everything pro, then your database AND cache folder can explode. We had a site with only around 2000 Products, but with 100 filterable product attributes.

This resulted in wp_rocket_cache table over 2 million entries and the cache folder going up to 200GB. To fix this, we decided to stop wp rocket from caching filtered WooCommerce category pages.

The following code snippet solved our problem:

add_filter( 'rocket_preload_exclude_urls', function( $regexes, $url ) {
    
    $url = untrailingslashit( $url );

    if(function_exists('is_filtered')){
        if ( is_filtered() ) {
            $regexes[] = $url;
            add_filter( 'do_rocket_generate_caching_files', '__return_false' );
        }
    }

    if(function_exists('flrt_is_filter_request')){
        if ( flrt_is_filter_request() ) {
            $regexes[] = $url;
            add_filter( 'do_rocket_generate_caching_files', '__return_false' );
        }
    }

    return $regexes;
}, 10, 2 );

Update 2025

In addition to the code above you should add this one below. Amazonbot for example is heavily crawling sites and you can block amazonbot with .htaccess and then also not cache filtered URLs + not generating CSS.

function disable_cache_for_filtered_urls() {

   if ( function_exists( 'flrt_is_filter_request' ) && flrt_is_filter_request() ) {
       if ( ! defined( 'DONOTCACHEPAGE' ) ) {
           define( 'DONOTCACHEPAGE', true ); // Disable WP Rocket caching
       }
   }

	if(function_exists('is_filtered') && is_filtered()){
       if ( ! defined( 'DONOTCACHEPAGE' ) ) {
           define( 'DONOTCACHEPAGE', true ); // Disable WP Rocket caching
       }
	}
}
add_action( 'template_redirect', 'disable_cache_for_filtered_urls' );

.htaccess code

# Block Harmful Robot

    RewriteEngine On
    RewriteCond %{HTTP_USER_AGENT} (amazonbot|semrushbot|ahrefsbot) [NC]
    RewriteRule (.*) - [F,L]

# END Block Harmful Robot

1 thoughts on “Prevent WP Rocket from Caching Filtered WooCommerce pages

Leave a Reply

Your email address will not be published. Required fields are marked *

We use cookies to give you the best online experience. By agreeing you accept the use of cookies in accordance with our cookie policy.

Close Popup
Privacy Settings saved!
Privacy Settings

When you visit any web site, it may store or retrieve information on your browser, mostly in the form of cookies. Control your personal Cookie Services here.

These cookies are necessary for the website to function and cannot be switched off in our systems.

Technical Cookies
In order to use this website we use the following technically required cookies
  • wordpress_test_cookie
  • wordpress_logged_in_
  • wordpress_sec

Decline all Services
Save
Accept all Services