Caching WordPress URLs with pretty permalinks and Google Analytics tags


#1

I have run into a problem with caching on WordPress sites. Here’s what happens:

An email is sent, reaching thousands of people. Several hundred will come to the site. The problem is that the URL is constructed like so:

http://www.website.com/nice-pretty-URL/?utm_source=Daily+Email&utm_campaign=312fa4-email060414&utm_medium=email&utm_term=0_c4f3c8d9d-313f4-22421373

The utm_term tag is unique to every recipient of the email (don’t ask, Mailchimp constructs the URL like that). Nginx then ends up caching hundreds of these URLs, because $request_uri is therefore “unique” to every link and caching is set as so:

fastcgi_cache_key “$scheme$request_method$host$request_uri”;

I’ve tried replacing this line with the following as per these instructions (link):

fastcgi_cache_key “$scheme$request_method$host$uri”;

or (with accompanying setup):

fastcgi_cache_key “$scheme$request_method$host$rt_uri”;

However, this breaks the site because $uri on a WordPress install is simply /index.php , at least that is what Nginx is spitting back when I debug using headers. And I found one possible solution using Lua, but reconfiguring the server and adding Lua seems like overkill.

So, I’m still looking for a way to strip out all the Google Analytics tags and force

http://www.website.com/nice-pretty-URL/?utm_source=Daily+Email&utm_campaign=312fa4-email060414&utm_medium=email&utm_term=0_c4f5h8d9d-312f4-22421373

to show the cached version of

http://www.website.com/nice-pretty-URL/

Hopefully my question makes sense. Any help is greatly appreciated! Thanks. :slight_smile:


#2

Really sorry for delayed reply.

I think in your case nginx is skipping cache because mailchimp url has query string.

You config may have a line like:

  
        if ($query_string != "") { set $skip_cache 1; }  

Add following a line after above line:

  
        if ( $arg_utm_source != "" ) { set $skip_cache 0; }  

I am not able to recall much about - https://rtcamp.com/support/reply/reply-to-cache-urls-which-use-only-javascript-related-queries-7/ but if your problem persist, just let me know.

I will try to recreate situation and try to find a “universal” workaround as this may affect many more users.


#3