LOG IN SIGN UP
Documentation

Query string manipulation VCL features

  Last updated October 20, 2017

Fastly provides a number of extensions to VCL, including several functions for query-string manipulation based on Dridi Boukelmoune's vmod-querystring for Varnish.

Functions

Function Description Example
querystring.add(<string>, <string>, <string>) Returns the given URL with the given parameter name and value appended to the end of the query string. The parameter name and value will be URL-encoded when added to the query string. set req.url = querystring.add(req.url, "foo", "bar");
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.set(<string>, <string>, <string>) Returns the given URL with the given parameter name set to the given value, replacing the original value and removing any duplicates. If the parameter is not present in the query string, the parameter will be appended with the given value to the end of the query string. The parameter name and value will be URL-encoded when set in the query string. set req.url = querystring.set(req.url, "foo", "baz");
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.regfilter_except as follows:

import querystring;

sub vcl_recv {
    # return this URL with only the parameters that match this regular expression
    set req.url = querystring.regfilter_except(req.url, "^(q|p)$");
}

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)");

Back to Top