Skip to content

Instantly share code, notes, and snippets.

@kfriend
Last active March 5, 2016 22:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kfriend/079ea35f6f496d053a5f to your computer and use it in GitHub Desktop.
Save kfriend/079ea35f6f496d053a5f to your computer and use it in GitHub Desktop.
Wordfence Falcon Homepage Workaround
<?php
// Wordfence Cache Workaround
// Apache 2.4.? - 2.4.8 contains a bug between mod_dir and mod_rewrite that prevents Wordfence's Falcon
// caching from working on the homepage, but just the homepage. This is a workaround for that issue.
$targetDomain = 'www.yourdomain.com';
// We'll need to check cookie existence to determine if the visitor is logged in. Some WP cookie names
// include a unique hash at the end, so we'll need to check by collecting all the cookie names, and
// passing them through a regex.
$cookieNames = preg_quote(implode(' ', array_keys($_COOKIE)), '/');
$cookieNameRegex = '/(comment_author|wp-postpass|wpmp_switcher|wordpress_logged_in_[^ ]+)/';
if (
// Only show cache with HTTP GET
strtolower($_SERVER['REQUEST_METHOD']) === 'get' &&
// Check to make sure UA is requesting the target domain
!empty($_SERVER['HTTP_HOST']) &&
strtolower($_SERVER['HTTP_HOST']) === strtolower($targetDomain) &&
// Is this a homepage request?
$_SERVER['REQUEST_URI'] === '/' &&
// Not checking wf_logout cookie at this time. Always seems to be '1' if you've logged in before.
// (isset($_COOKIE['wf_logout']) && $_COOKIE['wf_logout'] === '1')
// Check for WP-generated cookie existence
!preg_match($cookieNameRegex, $cookieNames)
)
{
// Detect if we should use gzip version
$gzip = (isset($_SERVER['HTTP_ACCEPT_ENCODING']) && strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== false);
// Detect if a cache file exists. Serve up gzip version if the UA supports gzip.
$file = __DIR__."/wp-content/wfcache/".$targetDomain."_/~~~~_wfcache.html" . ($gzip ? '_gzip' : '');
if (file_exists($file))
{
$cache = file_get_contents($file);
if ($gzip)
{
// Need to supply these headers for gzip to work as expected
header('Content-Encoding: gzip');
header('Content-Length: '.strlen($cache));
}
echo $cache;
exit;
}
}
unset($targetDomain, $cookieNames, $cookieNameRegex);
/**
* Front to the WordPress application. This file doesn't do anything, but loads
* wp-blog-header.php which does and tells WordPress to load the theme.
*
* @package WordPress
*/
/**
* Tells WordPress to load the WordPress theme and output it.
*
* @var bool
*/
define('WP_USE_THEMES', true);
/** Loads the WordPress Environment and Template */
require( dirname( __FILE__ ) . '/wp-blog-header.php' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment