This is an old revision of the document!
Drop certain query string parameters based on rules to make request cache friendly
A new feature has been added to 5.2.3 to drop certain query string parameters based on rules to make some request more cache friendly.
Newly added Apache style configuration directive is CacheKeyModify …
, currently it only can modify query string part. some examples:
CacheKeyModify -qs:utm*
to drop all query strings that the name part starts with “utm” .
CacheKeyModify -qs:utm
to drop query string with name exact matches “utm”.
CacheKeyModify clear
to discard all previous configurations.
The directive can be added to Apache server, vhost and .htaccess level.
This function is suit for users who's site bring junk query string, e.g. UTM code, and get too many different URLs which should store and serve with same cache but not.
Let's introduce a little about what is a UTM code?
A UTM code is a simple code that you can attach to a custom URL in order to track a source, medium, and campaign name. This enables Google Analytics to tell you where searchers came from as well as what campaign directed them to you.
Starting with version 5.2.3, LSWS has a built-in Drop certain query string parameters based on rules system.
How to Enable LSWS Drop Query String Feature on cPanel
As long as LSWS version is 5.2.3 or above, the LSWS Drop certain query string feature is enabled by default and does not need any extra configuration in the LSWS WebAdmin GUI or in Apache configurations.
You may wish to override the default settings at the server level, virtual-host level or even the .htaccess
level. Before making any changes.
Let's look at some examples for a WHM/cpanel EA4 environment:
After you run the following, the drop query string feature will be automatically enabled globally:
/usr/local/lsws/admin/misc/lsup.sh -f -v 5.2.3 (or above version)
You may wish to set a rule. You will need to set it at the server level of the Apache configuration file:
vi /etc/apache2/conf.d/includes/pre_main_global.conf
and add:
<IfModule Litespeed> CacheKeyModify -qs:utm* </IfModule>
This will drop all query strings that the name part starts with “utm” for all virtual hosts.
You can also drop query string with name exact matches “utm”:
<IfModule Litespeed> CacheKeyModify -qs:utm </IfModule>
No matter how the server level is set, the end user has the ability to clear it through .htaccess
by adding the following:
<IfModule Litespeed> CacheKeyModify clear </IfModule>
To verify the server and virtual host level settings, you may run the following command:
cd /etc/apache2/ grep -i -r CacheKeyModify
The design logic looks like the following:
We use A
,B
,C
instead of rules.
Server Level | VHost Level | .htaccess | Result |
---|---|---|---|
A | not set | not set | A |
A | not set | C | A+C |
A | B | not set | A+B |
A | B | C | A+B+C |
- The addition feature may not be fully implemented yet on v 5.2.3, but will be fully implemented on next release.
How to check
Prepare URL with junk query string
Assume we have a public WordPress site with domain testquerystring.com
and LSCache enabled. And use Campaign URL Builder, e.g. Campaign URL Builder or other UTM plugin to generate URL, which will looks like this:
Now, I can access my site with both of URLs:
- https://testquerystring.com
- https://testquerystring.com/?utm_source=google&utm_medium=email&utm_campaign=promo%20code
Setup rules to drop query string
For testing purpose, we can simply add the following to .htaccess file
CacheKeyModify -qs:utm_medium
Verify from dev tool
- Before setup rules:
- All king of query strings with same domain will store with different cache key. Above URLs will store 2 cache files
- After setup rules:
- All king of query strings with same domain will store with same cache key. Above URLs will store only one cache files
- I can access these urls and check cache are hit at first time visit.
- https://testquerystring.com/?utm_source=google&utm_medium=email1&utm_campaign=promo%20code
- https://testquerystring.com/?utm_source=google&utm_medium=email12&utm_campaign=promo%20code
- https://testquerystring.com/?utm_source=google&utm_medium=email123&utm_campaign=promo%20code
- https://testquerystring.com/?utm_source=google&utm_medium=email1234&utm_campaign=promo%20code
Verify from debug log
We can only check keywords QS
& KEY
with CACHE
tag from debug log:
tail -f /etc/apache2/logs/* | grep 'CACHE' | grep 'QS\|KEY'
Verify utm_medium
has been removed from CacheKey data → QS
[CACHE] Remove exact matched QS key [utm_medium], [CACHE] modified QS in cache key is [], [CACHE] CacheKey data: URI [/testquerystring.com/?], QS [utm_source=google&utm_campaign=promo%20code], Vary Cookie [_lscache_vary=xxxxx], Private Cookie [wp_woocommerce_session_xxxxx], IP [x.x.x.x]
More Flexibility
It also can be set via rewrite rule and it support multiple env combined.
For example, we are going to remove exactly matchutm_source
and prefix match utm_medium
Rewritecond %{QUERY_STRING} 'utm_source=google&utm_medium=email1&utm_campaign=promo%20code' RewriteRule .* - [E=cache-key-mod:-qs:utm_source, E=cache-key-mod:-qs:utm_medium*]
Logs shows only utm_campaign
left from query string
Remove exact matched QS key [utm_source], Remove prefix matched QS key [utm_medium], CacheKey data: URI [/wordpress/?], QS [utm_campaign=promo%20code],