Removing certain query string parameters only
I had a need to remove the following query string parameters but leaving all other intact.
- utm_campaign
- utm_medium
- utm_source
- utm_content
- utm_term
- mkt_tok
Here’s the VCL solution I came up with, adding to Fastly’s default VCL:
import boltsort;
sub vcl_recv {
# Request URL contains a query string
if (req.url ~ "\?") {
# Does request URL contain utm_* or mkt_tok parameters
if (req.url ~ "(utm_[a-z]+|mkt_tok)") {
# Remove key/value pair and optional trailing &
set req.url = regsuball(req.url, "(?:utm_[a-z]+|mkt_tok)=[^&]+&?", "");
# Remove trailing & or only ?
set req.url = regsuball(req.url, "\??&?$", "");
}
# Sort remaining parameters
set req.url = boltsort.sort(req.url);
}
#FASTLY recv
if (req.request != "HEAD" && req.request != "GET" && req.request != "FASTLYPURGE") {
return(pass);
}
return(lookup);
}
If you want to test it out, add the following to vcl_deliver
and look for the x-url header in the response, and it’ll show you the request URL sent to the origin:
set resp.http.x-url = req.url;
-
There's a better way of doing this now:
import querystring; /* remove client-side tracking parameters from querystring */ if (req.url ~ "\?") { set req.url = querystring.clean(req.url); set req.url = querystring.regfilter(req.url, "utm_[a-z]+|mkt_tok|gclid"); set req.url = querystring.sort(req.url); }
Please sign in to leave a comment.
Comments
1 comment