Remove Google Adwords gclid Query String for nginx

Using Google AdWords to gain traffic for your WordPress or WooCommerce site can increase loyal viewers and customers. You may find visitors who click your Google ad get a slower experience. Google adds a query string that looks like ?gclid=vak234kasd1312 which bypasses any caching you may have enabled. Google uses this query string for tracking, however, Google tracks using client side javascript which means we can remove this Google AdWords query string, deliver faster pages to clients and still track using Google Analytics.

This guide is for removing gclid query strings with nginx, the Apache .htaccess is here. A Varnish version will be published in the future.

Speed Tests with and without Google Adwords Query String

Using pingdom, the front page of this site loads in 339 ms without the query string

When repeating the speed test adding the ?gclid=va134e21adsfaf query string loads in 1.08 seconds because the cache is bypassed.

Read on to learn how to remove this query string using nginx rewrites for WordPress.

This could affect any marketing tracking scripts you are using, if you need tracking scripts to work with your nginx setup please get in touch with me here to discuss possibilities

Remove Google Adwords gclid Query String for nginx

In your nginx virtual host file, usually in /etc/nginx/sites-available or /etc/nginx/vhosts and add this snippet

# Remove gclid query string
if ($args ~* "gclid") {
    #? in uri? drops the gclid query string
    rewrite ^(.*)$ $uri? permanent;

Save the virtual host and then verify the nginx configuration syntax is OK.

sudo nginx -t

Reload the nginx service if there were no syntax errors.

sudo service nginx reload

Now when you test a URL with the ?gclid query string you will see it is removed.


nginx Strip Query String Rewrite
Remove Parameters with nginx rewrites
nginx http rewrite module