Server-based Page Caching

Page Caching in SWIS is already about as fast as it can possibly be. But there can still be a small amount of overhead from running PHP. To bypass PHP entirely, you can either integrate SWIS with a server-based cache. Or, you can implement rewrite rules with your server that will deliver the cached pages without running any PHP code.

But before we get to that, there are a few caveats. First, modern PHP is so fast, you may not see any difference. In my own tests the difference with/without PHP was a mere 2ms, but every site is different, so you can try it out and see for yourself.

Second, when using rewrite rules to deliver the cached files, there is no cache expiration, since no PHP runs and SWIS can't check the expiration time against the cached page. There's a way around that, but we'll get to that in a minute.

LiteSpeed

As of SWIS 2.7.0, LiteSpeed integration is built-in if your server already has server-side page caching enabled. You can replace the LS Cache plugin with SWIS Performance and it will automatically purge the LiteSpeed cache when you make changes to posts/pages, activate plugins, etc.

If, for some reason, you don't want SWIS to do anything with the LiteSpeed cache, you can add this to your wp-config.php:

define( 'SWIS_DISABLE_LITESPEED_INTEGRATION', true );

If your LiteSpeed server doesn't have caching enabled, and you don't have access to enable it, you can use the fall-back rewrite rules below.

Varnish

SWIS has automatic Varnish purging built-in, but by default it is only enabled for Cloudways sites. As such, SWIS can be used in place of the Breeze plugin when you enable Page Caching in the SWIS Performance settings.

Most other hosts that use Varnish have auto-purging plugins already installed. But, if you're using another Varnish setup that doesn't have an integration plugin, we have filters and a drop-in plugin available to integrate with any Varnish cache.

If your site is on a web host that uses Varnish but doesn't have their own integration, give us a holler and we can look into making SWIS work automatically there too.

Apache Rewrite Rules (and fall-back for LiteSpeed)

Add the following at the top of your .htaccess file. The main thing is to make sure it is before the # BEGIN WordPress   section. If you're using the WebP Variant option, be sure to uncomment that section, and you can also adjust the query string and cookie exclusions too.

If you have installed WordPress in a sub-directory, or are using a custom folder for the SWIS cache, you'll want to update the path in the last couple lines.

# BEGIN SWIS Cache
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /

    RewriteRule ^ - [E=SWIS_SCHEME:http-]
    RewriteCond %{HTTPS} on [OR]
    RewriteCond %{SERVER_PORT} ^443$ [OR]
    RewriteCond %{HTTP:X-Forwarded-Proto} https [OR]
    RewriteCond %{HTTP:X-Forwarded-Scheme} https
    RewriteRule ^ - [E=SWIS_SCHEME:https-]

    # Use this for the WebP Variant setting.
    # RewriteRule ^ - [E=SWIS_WEBP]
    # RewriteCond %{HTTP_ACCEPT} image/webp
    # RewriteRule ^ - [E=SWIS_WEBP:-webp]

    RewriteCond %{REQUEST_METHOD} GET

    # Use the second line if you want to allow any query strings.
    RewriteCond %{QUERY_STRING} =""
    # 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)).+$
RewriteCond %{HTTP_COOKIE} !(wp-postpass|wordpress_logged_in|comment_author|edd_cart_messages|edd_items_in_cart|edd_saved_cart|sc_checkout_id|sc_customer_id|sc_order_id|woocommerce_items_in_cart|wp_woocommerce_session)
    RewriteCond "%{DOCUMENT_ROOT}/wp-content/swis/cache/html/%{HTTP_HOST}%{REQUEST_URI}%{ENV:SWIS_SCHEME}index.html" -f
    RewriteRule ^ "/wp-content/swis/cache/html/%{HTTP_HOST}%{REQUEST_URI}%{ENV:SWIS_SCHEME}index.html" [L]
</IfModule>
# END SWIS Cache

Nginx Rewrite Rules

Add the following to your Nginx configuration, and uncomment the WebP Variant portion if needed. You may also customize the cookie/query-string exclusions as well. Note that the query string exclusions are those which are allowed to have a cached response.

Lastly, make sure the cache directory matches your setup, in case you have WordPress installed in a sub-directory or anything like that.

    # cache directory
    set $swis_cache_dir '/wp-content/swis/cache/html/';

    # default cache keys
    set $swis_cache_key_scheme      'http-';
    set $swis_cache_key_webp        '';

    # scheme cache key
    if ( $scheme = https ) {
        set $swis_cache_key_scheme 'https-';
    }
    if ( $server_port = 443 ) {
        set $swis_cache_key_scheme 'https-';
    }
    if ( $http_x_forwarded_proto = https ) {
        set $swis_cache_key_scheme 'https-';
    }
    if ( $http_x_forwarded_scheme = https ) {
        set $swis_cache_key_scheme 'https-';
    }

    # webp cache key
    # if ( $http_accept ~ image/webp ) {
    #     set $swis_cache_key_webp '-webp';
    # }

    # get cache file
    set $swis_cache_file_dir  ${swis_cache_dir}${http_host}${request_uri};
    set $swis_cache_file_name ${swis_cache_key_scheme}index${swis_cache_key_webp}.html;
    set $swis_cache_file      ${swis_cache_file_dir}${swis_cache_file_name};

    location / {
        error_page 405 = @fallback;
        recursive_error_pages on;

        # check request method
        if ( $request_method != GET ) {
            return 405;
        }

        # check excluded query strings
        if ( $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)).+$ ) {
            return 405;
        }

        # check excluded cookies
        if ( $http_cookie ~ (wp-postpass|wordpress_logged_in|comment_author|edd_cart_messages|edd_items_in_cart|edd_saved_cart|sc_checkout_id|sc_customer_id|sc_order_id|woocommerce_items_in_cart|wp_woocommerce_session) ) {
            return 405;
        }

        # deliver cache file if it exists
        try_files $swis_cache_file @fallback;
    }

    location @fallback {
        try_files $uri $uri/ /index.php?$args;
    }

Purge Cache with Cron

If you want to cleanup cache files automatically, or are using server-based rewrites, you can remove expired files with this command. Be sure to update the path and the time (in minutes):

/usr/bin/find /home/user/public_html/wp-content/swis/cache/html/ -type f -iname '*.html' -amin +1440 -print0 2>/dev/null | /usr/bin/xargs -0 rm -f

Also note that you might need to adjust the paths for find   and xargs  . I have a habit of using absolute paths in my cron jobs, and these might not match your system.

To Expire or Not

Do note that SWIS automatically purges the cache when certain actions occur, like post/page updates, theme changes, or plugin activations. So it may not be necessary to have a cache expiration/cleanup at all. And of course, you can always manually purge the cache if needed.

Still need help? Contact Us Contact Us