Skip to content

Instantly share code, notes, and snippets.

@brettbatie
Last active May 5, 2023 21:47
Show Gist options
  • Save brettbatie/18a37567f6742d1bb6fde50ae8f33f11 to your computer and use it in GitHub Desktop.
Save brettbatie/18a37567f6742d1bb6fde50ae8f33f11 to your computer and use it in GitHub Desktop.
Cache Enabler for Wordpress. .htaccess config modified from https://www.keycdn.com/support/wordpress-cache-enabler-plugin#apache
# BEGIN Cache Enabler
<IfModule mod_rewrite.c>
RewriteEngine On
# cache directory
RewriteRule ^ - [E=CE_CACHE_DIR:/wp-content/cache/cache-enabler]
# default cache keys
RewriteRule ^ - [E=CE_CACHE_KEY_SCHEME:http-]
# scheme cache key
RewriteCond %{HTTPS} ^(on|1)$ [OR]
RewriteCond %{SERVER_PORT} =443 [OR]
RewriteCond %{HTTP:X-Forwarded-Proto} =https [OR]
RewriteCond %{HTTP:X-Forwarded-Scheme} =https
RewriteRule ^ - [E=CE_CACHE_KEY_SCHEME:https-]
# device cache key
# RewriteCond %{HTTP_USER_AGENT} (Mobile|Android|Silk/|Kindle|BlackBerry|Opera Mini|Opera Mobi)
# RewriteRule ^ - [E=CE_CACHE_KEY_DEVICE:-mobile]
# webp cache key
RewriteCond %{HTTP:Accept} \bimage/webp\b
RewriteRule ^ - [E=CE_CACHE_KEY_WEBP:-webp]
# compression cache key
<IfModule mod_mime.c>
RewriteCond %{HTTP:Accept-Encoding} gzip
RewriteRule ^ - [E=CE_CACHE_KEY_COMPRESSION:.gz]
AddType text/html .gz
AddEncoding gzip .gz
</IfModule>
# get cache file
RewriteRule ^ - [E=CE_CACHE_FILE_DIR:%{ENV:CE_CACHE_DIR}/%{HTTP_HOST}]
RewriteRule ^ - [E=CE_CACHE_FILE_NAME:%{ENV:CE_CACHE_KEY_SCHEME}index%{ENV:CE_CACHE_KEY_DEVICE}%{ENV:CE_CACHE_KEY_WEBP}.html%{ENV:CE_CACHE_KEY_COMPRESSION}]
RewriteRule ^ - [E=CE_CACHE_FILE:%{ENV:CE_CACHE_FILE_DIR}/%{ENV:CE_CACHE_FILE_NAME}]
# check request method
RewriteCond %{REQUEST_METHOD} ^GET|HEAD$
# URL ends with / or .php
RewriteCond %{REQUEST_URI} (/|.php)$
# check excluded query strings
RewriteCond %{QUERY_STRING} !nocache [NC]
RewriteCond %{QUERY_STRING} !^(.*?((fbclid|ref|mc_(cid|eid))|(utm_(source|medium|campaign|term|content|expid))|(gclid|fb_(action_ids|action_types|source))|(age-verified|usqp|cn-reloaded|_ga|_ke))).+?$
# check excluded cookies
RewriteCond %{HTTP_COOKIE} !(wp-postpass|wordpress_logged_in|comment_author)_
RewriteCond %{THE_REQUEST} !^\s(/_test/|/wp-json/.*|/wp-admin/.*|/xmlrpc.php.*|/wp-login.php.*)$ [NC]
# check if cache file exists
RewriteCond %{DOCUMENT_ROOT}%{ENV:CE_CACHE_DIR}/%{HTTP_HOST}/$1/%{ENV:CE_CACHE_FILE_NAME} -f
# check permalink structure has trailing slash
# RewriteCond %{REQUEST_URI} /[^\./\?]+(\?.*)?$
# check permalink structure has no trailing slash
# RewriteCond %{REQUEST_URI} /[^\./\?]+/(\?.*)?$
# deliver cache file
RewriteRule ^(.*)$ %{ENV:CE_CACHE_DIR}/%{HTTP_HOST}/$1/%{ENV:CE_CACHE_FILE_NAME} [L,END]
</IfModule>
# END Cache Enabler
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment