Caching a Custom or AI-Built Website with LiteSpeed

Caching a Custom or AI-Built Website with LiteSpeed

Learn how to switch on LiteSpeed page caching for a website that has no cache plugin, such as a custom PHP website or one built with an AI tool. A few lines in your .htaccess file tell the server to save a copy of each page and reuse it.

Steps

  1. Log in to Plesk via your Zeniar Portal
  2. Select Files from the left-hand menu and open the folder your domain loads, usually httpdocs or httpdocs/public
  3. Click the .htaccess file to open it in the code editor. If there is no such file, click Add New, select Create File and name it .htaccess
  4. Add the following lines at the top of the file
    <IfModule LiteSpeed>
    RewriteEngine On
    RewriteCond %{REQUEST_METHOD} ^(GET|HEAD)$
    RewriteCond %{REQUEST_URI} !^/(admin|login|account|cart|checkout)
    RewriteRule .* - [E=Cache-Control:max-age=300]
    </IfModule>
  5. Change the names inside the brackets to the parts of your website that must never be saved, separated by upright bars
  6. Click Save
  7. Check that your pages are being cached. See Checking Whether LiteSpeed Cache Is Working

Important: Only use this on pages that are the same for every visitor. A saved page is shown to everyone, so a page that displays a name, an order or a form with a security token must be left out.

Please note: The number after max-age is how many seconds a saved page is kept. With 300, a change to your website can take up to five minutes to appear.

Your website's pages are now saved and served from the LiteSpeed cache.

If Your Website Has a Login

If visitors can log in, the server must also be told not to show saved pages to them. Find the name of the cookie your website sets at login, then add two lines using that name. Add the first line directly under RewriteEngine On, and the second directly above the final RewriteRule line.

RewriteRule .* - [E=Cache-Vary:your_login_cookie]
RewriteCond %{HTTP_COOKIE} !your_login_cookie

Clearing the Cache

  • Saved pages are replaced by themselves once the max-age period ends
  • To clear them straight away, your website can send the header X-LiteSpeed-Purge: * from any PHP page. If you built the project with an AI tool, ask it to send that header whenever content is saved

Other Types of Website