How to Exclude URLs from Caching in WP Super Cache: A Guide to Rejected Strings and DONOTCACHEPAGE
Content
One of the most common questions about the WP Super Cache plugin is how to prevent specific pages or posts from being cached. Whether you're running a multisite network or have a single site with dynamic content, knowing how to properly exclude URLs is crucial for site functionality. This guide will explain the two primary methods for excluding content: using the "Rejected URL Strings" setting and the DONOTCACHEPAGE PHP constant.
Understanding the "Rejected URL Strings" Setting
On the Advanced tab of the WP Super Cache settings, you'll find the "Rejected URL Strings" box. This is the most straightforward way to tell the plugin not to cache certain pages. However, its behavior depends entirely on how you format your entries.
- To match a complete slug: Surround the text with slashes. For example, entering
/test-123/will exclude only the page with the exact slug "test-123". It will not exclude pages with slugs like "my-test-123" or "test-123-page". - To match any URL containing a string: Enter the text without slashes. For example, entering
blogwill exclude any URL that contains the string "blog" anywhere within it, such as/my-blog/,/blogging-tips/, or/yourblog/.
Important Limitation: This setting only examines the part of the URL that comes after your domain name. It cannot be used to exclude an entire external or mapped domain on a multisite installation. For instance, adding https://blogname.com.ar or blogname.com.ar to the list will not work.
The Power of the DONOTCACHEPAGE Constant
For more advanced control, particularly from within a theme or a custom plugin, you can use the DONOTCACHEPAGE constant. This method is ideal for developers who need to programmatically disable caching based on complex conditions, such as the presence of a specific URL query string.
To use it, you need to add code to your theme's functions.php file or a custom plugin. The code should hook into an early action like init or template_redirect, check for your specific condition, and then define the constant.
Example Code Snippet:
function my_custom_dont_cache_condition() {
// Check for a specific query string
if ( isset( $_GET['specific-param'] ) ) {
define( 'DONOTCACHEPAGE', true );
}
// Check if the current page is a specific post slug
if ( is_page( 'test-123' ) ) {
define( 'DONOTCACHEPAGE', true );
}
}
add_action( 'template_redirect', 'my_custom_dont_cache_condition' );
When DONOTCACHEPAGE is defined as true, WP Super Cache will not save the cached version of that page during the request, ensuring the most dynamic content is always served.
Which Method Should You Use?
- For simple, static exclusions: Use the "Rejected URL Strings" box in the plugin's settings. It's user-friendly and doesn't require coding knowledge.
- For dynamic, conditional exclusions: Use the
DONOTCACHEPAGEmethod. This is necessary for excluding content based on query strings, user roles, or other programmatic logic. It is also the recommended workaround for excluding entire sites on a WordPress multisite network with mapped domains, as you can apply the logic site-wide through a theme.
By understanding the difference between these two powerful methods, you can fine-tune your caching rules to ensure a fast experience for most visitors while still allowing dynamic content to function perfectly where needed.
Related Support Threads Support
-
Excluding specific pages with slugs?https://wordpress.org/support/topic/excluding-specific-pages-with-slugs/
-
Documentation for REJECTED URL STRINGShttps://wordpress.org/support/topic/documentation-for-rejected-url-strings/
-
add to $cache_rejected_uri from pluginhttps://wordpress.org/support/topic/add-to-cache_rejected_uri-from-plugin/
-
Accepted Filenames & Rejected URIshttps://wordpress.org/support/topic/accepted-filenames-rejected-uris/
-
Rest API Docshttps://wordpress.org/support/topic/rest-api-docs/
-
Exclude cache of URLhttps://wordpress.org/support/topic/exclude-cache-of-url/