背景: 我们需要Wordpress插件WP Super Cache来忽略某些查询字符串,仅用于客户端Google分析,这些是utm_标签和gclid。
我们想尝试WP Super Cache,因为它支持移动和非移动的不同缓存。 W3总缓存没有。
结果是,无论这些查询字符串是否存在于url中,无论它们的值是什么,都将显示相同的缓存页面(如果该缓存不存在,它将为url创建一个新的缓存(省略了这些查询字符串参数)
Wordpress插件W3 Total Cache允许您添加要忽略的查询字符串,然后将其添加到.htaccess中,如下所示(包括其他相关的重写规则。我们正是将其从W3 Total Cache .htaccess复制到了WP超级缓存.htaccess
RewriteRule ^ - [E=W3TC_QUERY_STRING:%{QUERY_STRING}]
RewriteCond %{ENV:W3TC_QUERY_STRING} ^(.*?&|)utm_campaign=.*?(&.*|)$ [NC]
RewriteRule ^ - [E=W3TC_QUERY_STRING:%1%2]
RewriteCond %{ENV:W3TC_QUERY_STRING} ^(.*?&|)utm_source=.*?(&.*|)$ [NC]
RewriteRule ^ - [E=W3TC_QUERY_STRING:%1%2]
RewriteCond %{ENV:W3TC_QUERY_STRING} ^(.*?&|)utm_medium=.*?(&.*|)$ [NC]
RewriteRule ^ - [E=W3TC_QUERY_STRING:%1%2]
RewriteCond %{ENV:W3TC_QUERY_STRING} ^(.*?&|)utm_term=.*?(&.*|)$ [NC]
RewriteRule ^ - [E=W3TC_QUERY_STRING:%1%2]
RewriteCond %{ENV:W3TC_QUERY_STRING} ^(.*?&|)utm_content=.*?(&.*|)$ [NC]
RewriteRule ^ - [E=W3TC_QUERY_STRING:%1%2]
RewriteCond %{ENV:W3TC_QUERY_STRING} ^&+$
RewriteRule ^ - [E=W3TC_QUERY_STRING]
RewriteCond %{ENV:W3TC_QUERY_STRING} =""
并且也在下面添加:
RewriteCond %{REQUEST_URI} \/$ # also added from w3 super cache
因为我们注释掉了WP Super Cache的某些行。
# RewriteCond %{REQUEST_URI} !^.*[^/]$ # commented out from wp super cache
# RewriteCond %{REQUEST_URI} !^.*//.*$ # commented out from wp super cache
# RewriteCond %{QUERY_STRING} ^$ # commented out from wp super cache
但是,它不起作用。
所以我很确定它归结到最后的重写行。就是这样
RewriteCond %{DOCUMENT_ROOT}/wp-content/cache/supercache/%{SERVER_NAME}/$1/index-https.html.gz -f
RewriteRule ^(.*) "/wp-content/cache/supercache/%{SERVER_NAME}/$1/index-https.html.gz" [L]
因此,我认为也许(。*)仍会捕获所有查询字符串,并尝试将其传递给重写规则。因此,将其替换为%{REQUEST_URI},就像W3的总缓存一样:
RewriteCond %{DOCUMENT_ROOT}/wp-content/cache/supercache/%{SERVER_NAME}/%{REQUEST_URI}/index-https.html.gz -f
RewriteRule ^.* "/wp-content/cache/supercache/%{SERVER_NAME}/%{REQUEST_URI}/index-https.html.gz" [L]
W3总缓存如下:
RewriteCond "%{DOCUMENT_ROOT}/wp-content/cache/page_enhanced/%{HTTP_HOST}/%{REQUEST_URI}/_index%{ENV:W3TC_SSL}%{ENV:W3TC_PREVIEW}.html" -f
RewriteRule .* "/wp-content/cache/page_enhanced/%{HTTP_HOST}/%{REQUEST_URI}/_index%{ENV:W3TC_SSL}%{ENV:W3TC_PREVIEW}.html" [L]
仍然无效。意思是,对于这些查询字符串还是没有查询字符串,仍然有不同的缓存。对于同一查询字符串变量,仍然具有不同值的不同缓存。