LiteSpeed is a high-performance web server that includes built-in page caching. If your hosting provider uses LiteSpeed (many shared hosts do), you can get significant performance improvements with just a few .htaccess directives. Here's the configuration I use at DailyWatch.
Basic LiteSpeed Cache Setup
Add this to your .htaccess file:
<IfModule LiteSpeed>
# Enable public caching
CacheEnable public /
CacheLookup on
# Default TTL: 1 hour
CacheDefaultExpire 3600
# Don't cache admin or API routes
RewriteEngine On
RewriteRule ^admin - [E=Cache-Control:no-cache]
RewriteRule ^api/ - [E=Cache-Control:no-cache]
RewriteRule ^task/ - [E=Cache-Control:no-cache]
</IfModule>
The <IfModule LiteSpeed> block ensures these directives only apply when LiteSpeed is the server. Apache will safely ignore the entire block.
Route-Specific Cache TTLs
Different pages have different freshness requirements:
<IfModule LiteSpeed>
CacheEnable public /
CacheLookup on
# Home page and category pages: 3 hours
<LocationMatch "^/(|category/)">
Header set Cache-Control "public, max-age=10800, stale-while-revalidate=7200"
</LocationMatch>
# Watch pages: 6 hours (video metadata rarely changes)
<LocationMatch "^/watch/">
Header set Cache-Control "public, max-age=21600, stale-while-revalidate=86400"
</LocationMatch>
# Search pages: 10 minutes (dynamic results)
<LocationMatch "^/search">
Header set Cache-Control "public, max-age=600, stale-while-revalidate=1800"
</LocationMatch>
# Static assets: 30 days
<LocationMatch "\.(css|js|png|jpg|svg|woff2)$">
Header set Cache-Control "public, max-age=2592000, immutable"
</LocationMatch>
</IfModule>
PHP-Level Cache Integration
LiteSpeed respects Cache-Control headers set by PHP, giving you programmatic control:
function setCacheHeaders(string $pageType): void {
$ttls = [
'home' => 10800, // 3h
'category' => 10800, // 3h
'watch' => 21600, // 6h
'search' => 600, // 10min
'channel' => 14400, // 4h
];
$maxAge = $ttls[$pageType] ?? 3600;
$swr = (int)($maxAge * 0.67); // stale-while-revalidate
header("Cache-Control: public, max-age={$maxAge}, stale-while-revalidate={$swr}");
header('Vary: Accept-Encoding');
}
Cache Purging After Deploy
When you deploy new code, the LiteSpeed cache may serve stale pages. Clear it after every deployment:
#!/bin/bash
# In your deploy script
lftp -e "rm -rf /public_html/lscache; quit" $FTP_HOST
echo "LiteSpeed cache cleared"
Or via PHP if you have access:
// Clear LiteSpeed cache programmatically
function clearLiteSpeedCache(): void {
$lscachePath = $_SERVER['DOCUMENT_ROOT'] . '/lscache';
if (is_dir($lscachePath)) {
$iterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($lscachePath, FilesystemIterator::SKIP_DOTS),
RecursiveIteratorIterator::CHILD_FIRST
);
foreach ($iterator as $file) {
$file->isDir() ? rmdir($file) : unlink($file);
}
}
}
Cloudflare Compatibility
If you use Cloudflare in front of LiteSpeed, be aware of the double-caching layer. Set appropriate s-maxage for CDN caching vs max-age for browser caching:
// CDN caches for 1 hour, browser for 3 hours
header('Cache-Control: public, max-age=10800, s-maxage=3600');
Measuring the Impact
Before LiteSpeed cache on dailywatch.video:
- Average response time: 150-200ms (with PHP file cache)
- Time to First Byte: 180ms
After LiteSpeed cache:
- Average response time: 20-40ms (served from LiteSpeed cache)
- Time to First Byte: 30ms
That's a 5-7x improvement from configuration alone, with zero code changes. If your host runs LiteSpeed, this is free performance waiting to be claimed.
The complete .htaccess configuration powering DailyWatch handles LiteSpeed caching, Cloudflare Flexible SSL, URL rewrites, and security headers in a single file.
Top comments (0)