Caching for sites not built on WordPress
If your site isn't WordPress, there's no LiteSpeed Cache plugin to install. You can still get most of the benefit with a few lines in .htaccess. These tell visitors' browsers to keep files and make downloads smaller.
Before you start
- Make a copy of your
.htaccessfile first. See .htaccess basics with examples. - Know what your site is. Static HTML, a PHP site, Laravel, and Joomla can all use the settings below. Use the app's own caching for dynamic pages.
Step 1: Browser caching
This tells browsers how long to keep each type of file before asking again.
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpeg "access plus 1 year"
ExpiresByType image/png "access plus 1 year"
ExpiresByType image/webp "access plus 1 year"
ExpiresByType image/svg+xml "access plus 1 year"
ExpiresByType font/woff2 "access plus 1 year"
ExpiresByType text/css "access plus 1 month"
ExpiresByType application/javascript "access plus 1 month"
ExpiresByType text/html "access plus 0 seconds"
</IfModule>
HTML is not cached by the browser, so visitors see new content right away. If you change a CSS or JavaScript file, rename it, or add a version number such as style.css?v=3, or visitors may keep the old one for a month.
Step 2: Compression
Compression shrinks text files, often by 70% or more. Our LiteSpeed servers already compress HTML, CSS, and JavaScript, so most sites don't need to add anything. If your app ships rules like these, they do no harm.
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/css text/plain text/xml
AddOutputFilterByType DEFLATE application/javascript application/json image/svg+xml
</IfModule>
Images in JPEG, PNG, and WebP are already compressed. Don't add them.
Step 3: Check it worked
- Open DevTools (
F12), then the Network tab. Reload. - Click a CSS file. In Response Headers, look for:
cache-control: max-age=2592000
content-encoding: br
content-encoding shows gzip or br (Brotli) when compression is on.
Step 4: Cache the dynamic part in your app
Server pages built on every visit, such as PHP, Laravel, or Node, should cache their output or database results in the app. Common options:
| App | What to use |
|---|---|
| Laravel | php artisan config:cache, route:cache, and view:cache |
| Joomla, Drupal | Built-in page cache and a cache extension |
| Custom PHP | Store expensive results in a file for a short time, or use OPcache for code. See PHP version and OPcache for speed |
| Node.js or Python | Cache responses in memory or in a file in your app |
LiteSpeed server cache
LiteSpeed's server-level page cache is on for every account. WordPress uses it through the LiteSpeed Cache plugin. Other apps can use it with cache rules in .htaccess, or by sending LiteSpeed's cache headers. If you'd like help setting up the rules for your app, open a request. See What to include when you ask for help.
Other wins that don't need caching
- Resize and compress images. See Optimize images for the web.
- Use modern formats such as WebP.
- Remove scripts you don't use.
- Put big, rarely-changing files behind a CDN. See Do you need a CDN?.
If something goes wrong
| Problem | Fix |
|---|---|
500 error after editing .htaccess |
A typo or an unsupported line. Restore your copy. See 500 Internal Server Error |
| Changes to CSS don't show | Version the file name, or clear the browser cache |
No content-encoding header |
Compression isn't active, or a CDN or proxy removes it. Ask us |
Still stuck? Ask us and we'll walk you through it.