Remove Google Adwords gclid Query String in .htaccess

Using Google AdWords to gain traffic for your WordPress or WooCommerce site is very common. If you get a lot of traffic then you may find visitors who click your Google ad get a slower experience. This is due to the query string ?gclid=vak234kasd1312 which bypasses any caching you may have enabled. This query string is used for tracking, however, Google does all tracking 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 Apache .htaccess. An nginx and 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 Apache’s .htaccess file for WordPress.

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

Remove Google Adwords gclid Query String in .htaccess

Make this the first rule in your .htaccess file. It captures the gclid query string and removes it (the trailing ? after $1 along with QSD)

# BEGIN Remove Google Adwords query string 
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} "gclid=" [NC]
RewriteRule (.*) /$1? [R=301,L,QSD]
<IfModule mod_rewrite.c>
# END Remove Google Adwords query string

Add the previous rule before your WordPress rules which usually look like this

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# END WordPress

Test by going to any of your landing pages and adding the suffix query string ?gclid=va134asf81q81 and you will see it get removed.


Rewrite URL to Remove gclid Query
Removing Query Strings from End of URL
Strip Adwords with Varnish
Remove Query String htaccess with QSA