WordPress .htaccess Performance: Enable Gzip Compression, Expires Headers, and Cache-Control

Compression and browser caching for WordPress in .htaccess

Two of the most common PageSpeed findings, “Enable text compression” and “Serve static assets with an efficient cache policy”, are fixed on the web server, not in WordPress. On Apache hosting, which most shared WordPress hosts still use, three modules do it: mod_deflate compresses text responses, mod_expires and mod_headers tell browsers how long to keep static files. WordPress adds a version to every enqueued file, so long caching is safe.

Problem: PageSpeed Insights reports uncompressed text files and short cache lifetimes for CSS, JavaScript and images on an Apache host without a caching plugin.

Solution: Add mod_deflate rules for text types and long Expires / Cache-Control headers for static files to .htaccess, keep HTML uncached by these rules, and make sure theme assets are enqueued with a version so updates reach visitors.

Compression

Add the blocks outside the # BEGIN WordPress section, which WordPress rewrites when permalinks are saved:

<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/html text/plain text/css text/xml
    AddOutputFilterByType DEFLATE application/javascript text/javascript application/json
    AddOutputFilterByType DEFLATE application/xml application/rss+xml image/svg+xml
</IfModule>

Images (JPEG, PNG, WebP), fonts in WOFF2 and archives are already compressed; listing them only costs CPU. Many hosts also support Brotli (mod_brotli), which compresses text better; if it is available, the host usually enables it for you.

Browser caching

<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresByType text/css                 "access plus 1 year"
    ExpiresByType application/javascript   "access plus 1 year"
    ExpiresByType text/javascript          "access plus 1 year"
    ExpiresByType image/jpeg               "access plus 6 months"
    ExpiresByType image/png                "access plus 6 months"
    ExpiresByType image/webp               "access plus 6 months"
    ExpiresByType image/svg+xml            "access plus 6 months"
    ExpiresByType image/x-icon             "access plus 1 year"
    ExpiresByType font/woff2               "access plus 1 year"
</IfModule>

<IfModule mod_headers.c>
    <FilesMatch "\.(css|js|woff2|svg|ico|jpe?g|png|webp|gif)$">
        Header set Cache-Control "public, max-age=31536000"
    </FilesMatch>
</IfModule>

HTML is deliberately not listed: WordPress sends its own headers for pages, and a long browser cache for HTML would show visitors old content after you publish.

Versions make long caching safe

A browser that keeps style.css for a year would never see your changes, unless the URL changes. WordPress adds ?ver= to every enqueued file; use the file’s modification time as the version for theme assets, so every change gets a new URL:

<?php
add_action( 'wp_enqueue_scripts', function () {
    $dir = get_stylesheet_directory();
    $uri = get_stylesheet_directory_uri();

    wp_enqueue_style( 'ha-style', $uri . '/assets/style.css', array(), filemtime( $dir . '/assets/style.css' ) );
    wp_enqueue_script( 'ha-app', $uri . '/assets/app.js', array(), filemtime( $dir . '/assets/app.js' ), true );
} );

Images uploaded to the media library get new file names when replaced, so they are safe to cache too. Files you replace by FTP under the same name (a logo, a PDF) are the exception: rename them or keep their cache time short.

Checking

curl -sI -H "Accept-Encoding: gzip" https://example.com/wp-content/themes/mytheme/assets/style.css | grep -iE "content-encoding|cache-control|expires"

The answer should contain Content-Encoding: gzip (or br) and the long Cache-Control. If nothing changes, the module is not enabled on the server, or the host overrides the headers: ask the host, or check whether a CDN in front of the site sets its own.

NOTE: On Nginx, .htaccess is ignored; the same settings are gzip on, gzip_types and expires blocks in the server configuration. And these headers only make repeat visits faster; for the first visit, page caching and smaller files matter more.