Invalid patterns of exclusions
Certain patterns of exclusions cannot be handled and are automatically removed.
This is because the exclusion fields are looking for a Regular Expression (regex) pattern and some characters will have a special meaning that can cause problems with the regex, and will generate a warning in the website's error log.
If you add the invalid patterns, you will see an admin notice with the following text:
WP Rocket: The following patterns are invalid and have been removed.
The following are the special characters that invalidate a pattern:
- backslash:
\
- caret:
^
- dollar sign:
$
- period or dot:
.
- vertical bar or pipe symbol:
|
- question mark:
?
- asterisk or star:
*
- plus sign:
+
- opening parenthesis:
(
- closing parenthesis:
)
- opening square bracket:
[
- opening curly brace:
{
If you need the Regular Expression to match literally any of the listed special characters, you should escape them by prepending them with a backslash \
.
Examples
The following are examples of exclusions with special characters correctly validated:
unmatched\)closing\)parenthesis
my\(exclusions\]pattern
In a specific example, to match a script like the following:
<script> window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); gtag('set', 'developer_id.dOGY3NW', true); gtag('config', 'UA-40478734-1', { 'allow_google_signals': false, 'link_attribution': false, 'anonymize_ip': true, 'custom_map': { 'dimension1': 'logged_in' }, 'logged_in': 'no' } );
Where gtag(
is used, and the parenthesis is part of the keyword you chose, you'll need to escape the parenthesis for the regular expression to match the script: gtag\(
.
But if you want to exclude all files in the /wp-content/
directory you'll need to use (.*)
without escaping, i.e. adding a backslash, the parenthesis:
/wp-content/(.*)(
However, if you really want it to be a regex, you should not escape them.