How to Set Up Browser Caching in Apache?

Implementing browser caching is one of the most effective ways to boost your website’s loading speed and reduce server bandwidth. By telling visitors’ browsers to store static files—like images, CSS, and JavaScript—locally, you eliminate the need for them to re-download the same assets on subsequent page views. This article provides a straightforward, step-by-step guide on how to configure browser caching using the Apache web server via the .htaccess file, utilizing both the mod_expires and mod_headers modules.

Step 1: Enable the Required Apache Modules

Before configuring caching rules, you must ensure that Apache has the necessary modules enabled. The two primary modules used for caching are mod_expires (for setting expiration dates) and mod_headers (for more granular control over cache headers).

If you have server root access (via SSH), you can enable these modules by running the following commands in your terminal:

sudo a2enmod expires
sudo a2enmod headers
sudo systemctl restart apache2

If you are on a shared hosting environment, these modules are usually enabled by default by your hosting provider.

Step 2: Locate or Create Your .htaccess File

Apache allows you to configure caching site-wide or per-directory using a .htaccess file.

Step 3: Configure Caching with mod_expires

The mod_expires module is the easiest and most common way to set up browser caching. It allows you to specify how long different file types should be cached relative to the time the visitor accesses the file.

Open your .htaccess file and add the following block of code:

<IfModule mod_expires.c>
    ExpiresActive On
    
    # Default expiration period
    ExpiresDefault "access plus 1 month"
    
    # Images
    ExpiresByType image/jpg "access plus 1 year"
    ExpiresByType image/jpeg "access plus 1 year"
    ExpiresByType image/gif "access plus 1 year"
    ExpiresByType image/png "access plus 1 year"
    ExpiresByType image/webp "access plus 1 year"
    ExpiresByType image/x-icon "access plus 1 year"
    
    # CSS and JavaScript
    ExpiresByType text/css "access plus 1 month"
    ExpiresByType text/javascript "access plus 1 month"
    ExpiresByType application/javascript "access plus 1 month"
    
    # Web Fonts
    ExpiresByType font/ttf "access plus 1 year"
    ExpiresByType font/woff "access plus 1 year"
    ExpiresByType font/woff2 "access plus 1 year"
    
    # HTML and XML (Keep short to ensure updates are seen)
    ExpiresByType text/html "access plus 0 seconds"
    ExpiresByType application/xml "access plus 0 seconds"
</IfModule>

Step 4: Add Cache-Control Headers with mod_headers

While mod_expires handles the duration, adding Cache-Control headers via mod_headers provides extra security and compatibility with modern web standards, ensuring proxies and browsers handle the cache correctly.

Append this additional code block to your .htaccess file:

<IfModule mod_headers.c>
    # Cache security and validation
    <FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|webp|js|css|swf)$">
        Header set Cache-Control "max-age=2592000, public"
    </FilesMatch>
</IfModule>

Step 5: Save and Verify Your Changes

Save the changes to your .htaccess file and upload it back to your server if you edited it locally.

To verify that your browser caching is working correctly, you can open your website in a browser, open the developer tools (F12), and navigate to the Network tab. Refresh the page, click on a static asset (like a CSS file or an image), and look at the Response Headers. You should see the Cache-Control and Expires headers reflecting the timeframes you specified in your configuration.