LOG IN SIGN UP
Documentation

Query string manipulation VCL features

Fastly provides several functions in VCL for query-string manipulation. It is based on Dridi Boukelmoune's vmod-querystring for Varnish — you can also refer to the documentation for more information.

Functions

Function Description Example
querystring.clean(<string>) Returns the given URL without empty parameters. The query-string is removed if empty (either before or after the removal of empty parameters). Note that a parameter with an empty value does not constitute an empty parameter, so a query string "?something" would not be cleaned. set req.url = querystring.clean(req.url);
querystring.filter(<string>, <string_list>) Returns the given URL without the listed parameters. set req.url = querystring.filter(req.url, "utm_source" + querystring.filtersep() + "utm_medium" + querystring.filtersep() + "utm_campaign");
querystring.filter_except(<string>, <string_list>) Returns the given URL but only keeps the listed parameters. set req.url = querystring.filter_except(req.url, "q" + querystring.filtersep() + "p");
querystring.filtersep() Returns the separator needed by the filter and filter_except functions.
querystring.globfilter(<string>, <string>) Returns the given URL without the parameters matching a glob. set req.url = querystring.globfilter(req.url, "utm_*");
querystring.globfilter_except(<string>, <string>) Returns the given URL but only keeps the parameters matching a glob. set req.url = querystring.globfilter_except(req.url, "sess*");
querystring.regfilter(<string>, <string>) Returns the given URL without the parameters matching a regular expression. set req.url = querystring.regfilter(req.url, "utm_.*");
querystring.regfilter_except(<string>, <string>) Returns the given URL but only keeps the parameters matching a regular expression. set req.url = querystring.regfilter_except(req.url, "^(q|p)$");
querystring.remove(<string>) Returns the given URL with its query-string removed. set req.url = querystring.remove(req.url);
querystring.sort(<string>) Returns the given URL with its query-string sorted. set req.url = querystring.sort(req.url);

Examples

In your VCL, you could use querystring.sort as follows:

import querystring;

sub vcl_hash {
  # sort the URL before the request hashing
  set req.url = querystring.sort(req.url);
}

You can use querystring.regfilter to specify a list of arguments that must not be removed (everything else will be) with a negative look-ahead expression:

set req.url = querystring.regfilter(req.url, "^(?!param1|param2)");