Close

February 6, 2019

WordPress Caching Tips for Any Webserver

One concern for page speed is when a user comes to your website using a CPC ad or affiliate ad. Those links usually have tracking parameters on them (ie: utm_content) and as a result can slow down a page being sent to the end user. What is worse, is that those links are usually dynamically generated, so each subsequent link will have to be rebuilt from the cache.

If your hosting is slow, this can be a killer situation.

Thankfully, there is a relatively easy solution we came up with that works well for both NGINX and APACHE webserver. It involves only editing one file on your blog. (Note this has only been tested with the WordPress Super Cache plugin, but in theory should work with other caching plugins).

In the “index.php” file, after the top comment, add the following

function strip_param_from_url( $url, $param )
{
    $base_url = strtok($url, '?');              // Get the base url
    $parsed_url = parse_url($url);              // Parse it
    $query = $parsed_url['query'];              // Get the query string
    parse_str( $query, $parameters );           // Convert Parameters into array
    unset( $parameters[$param] );               // Delete the one you want
    $new_query = http_build_query($parameters); // Rebuilt query string
    return (empty($parameters)) ? $base_url : $base_url . '?' . $new_query;            // Finally url is ready
}
 
//unset parameter list:
$BAD_PARM_LIST="msclkid,mkt_tok,affiliate_id,clickId,click_id,custom_affiliate_url,creative_details,utm_source,utm_campaign,utm_term,utm_content,utm_medium";
$list=explode(",",$BAD_PARM_LIST);
foreach($list as $item)
        $_SERVER["REQUEST_URI"]=strip_param_from_url($_SERVER['REQUEST_URI'],$item);

You will need to change the BAD_PARM_LIST based on what parameters you use for advertising/affiliate networks.
The will alter your REQUEST_URI path and remove any instances of the bad parameters you do NOT want to break your cache. The WP SuperCache plugin, uses the REQUEST_URI to get a cached copy of the webpage, so if those parameters do not exist, it will get the plain version of the page instead.

For instance:
http://YourSite.com?msclkid=123&affiliate_id=123
will actually look like
http://YourSite.com

to the backend of WordPress. Note that the URL does NOT actual get rewritten so any tagging manager you have on the site will STILL get executed properly. The user experience and page speed are very important elements to test for in site performance testing, so this is important to make sure you have it thoroughly tested.