{"id":16855,"date":"2023-12-22T08:12:53","date_gmt":"2023-12-22T07:12:53","guid":{"rendered":"https:\/\/www.welaunch.io\/en\/?p=16855"},"modified":"2025-03-10T19:46:13","modified_gmt":"2025-03-10T18:46:13","slug":"prevent-wp-rocket-from-caching-filtered-woocommerce-pages","status":"publish","type":"post","link":"https:\/\/www.welaunch.io\/en\/2023\/12\/prevent-wp-rocket-from-caching-filtered-woocommerce-pages\/","title":{"rendered":"Prevent WP Rocket from Caching Filtered WooCommerce pages"},"content":{"rendered":"
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.<\/p>\n
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.<\/p>\n
The following code snippet solved our problem:<\/p>\n
\nadd_filter( 'rocket_preload_exclude_urls', function( $regexes, $url ) {\n \n $url = untrailingslashit( $url );\n\n if(function_exists('is_filtered')){\n if ( is_filtered() ) {\n $regexes[] = $url;\n add_filter( 'do_rocket_generate_caching_files', '__return_false' );\n }\n }\n\n if(function_exists('flrt_is_filter_request')){\n if ( flrt_is_filter_request() ) {\n $regexes[] = $url;\n add_filter( 'do_rocket_generate_caching_files', '__return_false' );\n }\n }\n\n return $regexes;\n}, 10, 2 );\n<\/pre>\nUpdate 2025<\/p>\n
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.<\/p>\n
\nfunction disable_cache_for_filtered_urls() {\n\n if ( function_exists( 'flrt_is_filter_request' ) && flrt_is_filter_request() ) {\n if ( ! defined( 'DONOTCACHEPAGE' ) ) {\n define( 'DONOTCACHEPAGE', true ); \/\/ Disable WP Rocket caching\n }\n }\n\n\tif(function_exists('is_filtered') && is_filtered()){\n if ( ! defined( 'DONOTCACHEPAGE' ) ) {\n define( 'DONOTCACHEPAGE', true ); \/\/ Disable WP Rocket caching\n }\n\t}\n}\nadd_action( 'template_redirect', 'disable_cache_for_filtered_urls' );\n<\/pre>\n.htaccess code<\/p>\n
\n# Block Harmful Robot\n\n RewriteEngine On\n RewriteCond %{HTTP_USER_AGENT} (amazonbot|semrushbot|ahrefsbot) [NC]\n RewriteRule (.*) - [F,L]\n<\/IfModule>\n# END Block Harmful Robot\n<\/pre>\n","protected":false},"excerpt":{"rendered":"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…<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-16855","post","type-post","status-publish","format-standard","hentry","category-woocommerce"],"_links":{"self":[{"href":"https:\/\/www.welaunch.io\/en\/wp-json\/wp\/v2\/posts\/16855","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.welaunch.io\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.welaunch.io\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.welaunch.io\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.welaunch.io\/en\/wp-json\/wp\/v2\/comments?post=16855"}],"version-history":[{"count":2,"href":"https:\/\/www.welaunch.io\/en\/wp-json\/wp\/v2\/posts\/16855\/revisions"}],"predecessor-version":[{"id":19426,"href":"https:\/\/www.welaunch.io\/en\/wp-json\/wp\/v2\/posts\/16855\/revisions\/19426"}],"wp:attachment":[{"href":"https:\/\/www.welaunch.io\/en\/wp-json\/wp\/v2\/media?parent=16855"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.welaunch.io\/en\/wp-json\/wp\/v2\/categories?post=16855"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.welaunch.io\/en\/wp-json\/wp\/v2\/tags?post=16855"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}