One of the most important performance metrics for a WordPress website is Server Response Time, commonly measured as Time to First Byte (TTFB). While caching plugins like WP Rocket significantly improve performance, many server configurations still route every request through PHP before serving the cached page.
In reality, cached HTML files can be delivered directly by the web server (Apache or Nginx), completely bypassing PHP and WordPress. This approach reduces CPU usage, lowers the PHP-FPM workload, and improves overall server response time.
This guide explains how to optimize both Apache (.htaccess) and Nginx so they can serve WP Rocket's static HTML cache directly.
Why Is This Optimization Important?
By default, a typical WordPress request follows this flow:
Visitor
│
▼
Apache/Nginx
│
▼
PHP
│
▼
WordPress
│
▼
WP Rocket Cache
│
▼
HTML Response
Even when a page has already been cached, the request still passes through PHP before the cached content is returned.
With the following configuration, the request flow becomes:
Visitor
│
▼
Apache/Nginx
│
▼
WP Rocket HTML Cache
│
▼
HTML Response
PHP and WordPress are only executed when a cached file does not exist.
Benefits
- Lower Time to First Byte (TTFB)
- Reduced CPU usage
- Less PHP-FPM processing
- Better performance during traffic spikes
- Ideal for VPS and dedicated servers
- Improved scalability with minimal configuration changes
Apache (.htaccess) Optimization
If your server runs Apache, insert the following block inside the WordPress rewrite section, immediately after:
RewriteBase /
and before:
RewriteRule ^index\.php$ - [L]
The resulting configuration should look like this:
# BEGIN WordPress
# Die Anweisungen (Zeilen) zwischen „BEGIN WordPress“ und „END WordPress“ sind
# dynamisch generiert und sollten nur über WordPress-Filter geändert werden.
# Alle Änderungen an den Anweisungen zwischen diesen Markierungen werden überschrieben.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteCond %{REQUEST_METHOD} GET
RewriteCond %{QUERY_STRING} =""
RewriteCond %{HTTP:Cookie} !wordpress_logged_in
RewriteCond %{DOCUMENT_ROOT}/wp-content/cache/wp-rocket/%{HTTP_HOST}%{REQUEST_URI}/index.html -f
RewriteRule ^(.*)$ /wp-content/cache/wp-rocket/%{HTTP_HOST}/$1/index.html [L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
How It Works
This rewrite rule performs several checks before serving a cached page:
- Only processes GET requests.
- Ignores requests containing query strings.
- Skips logged-in WordPress users.
- Verifies that a cached HTML file exists.
- If the cache is available, Apache serves the static HTML file immediatel
Because the request never reaches PHP, WordPress is completely bypassed, resulting in significantly faster response times.
Nginx Optimization
For Nginx users, replace your existing location / block with the following configuration:
#
# Static cache
#
location / {
try_files \
/wp-content/cache/wp-rocket/$host$uri/index.html \
/wp-content/cache/wp-rocket/$host$uri.html \
$uri \
$uri/ \
@wordpress;
autoindex on;
}
#
# WordPress
#
location @wordpress {
rewrite ^ /index.php?$args last;
}
How It Works
The try_files directive checks each location in order:
- Cached directory
index.html- Cached HTML file
- Original requested file
- Directory
- Finally, falls back to WordPress
If a cached page is found, Nginx returns it immediately without invoking PHP-FPM.
This makes the request handling process considerably more efficient than routing every request through PHP.
Expected Performance Improvements
After applying these optimizations, you can typically expect:
- Lower Server Response Time (TTFB)
- Reduced CPU utilization
- Fewer PHP worker processes
- Better performance under high traffic
- Improved PageSpeed Insights and GTmetrix scores, particularly for the "Reduce initial server response time" metric
The actual improvement depends on your server specifications, installed plugins, and traffic volume, but websites with moderate to high traffic often experience noticeable gains.
Things to Keep in Mind
Before enabling these configurations, make sure:
- WP Rocket is installed and HTML page caching is working correctly.
- Cache files have already been generated through preload or user visits.
- Your website doesn't rely heavily on dynamic content that changes for every visitor.
- You back up your .htaccess or Nginx configuration before making changes.
Conclusion
Serving cached HTML files directly from Apache or Nginx is one of the simplest yet most effective ways to improve WordPress performance. By eliminating unnecessary PHP and WordPress processing for cached pages, your server can respond faster, consume fewer resources, and handle significantly more concurrent visitors.
If you're already using WP Rocket, adding these rewrite rules to Apache or the appropriate try_files configuration to Nginx is a straightforward optimization that can deliver measurable improvements in server response time with minimal effort.
Top comments (0)