htaccess Header set Cache-Control

htaccess Header set Cache-Control
htaccess Header set Cache-Control

Learn how to configure Cache-Control headers in your htaccess file to improve website performance by setting cache durations for images, CSS, and JavaScript files.

Caching plays a critical role in improving website performance by reducing the load on your server and speeding up page load times. One of the most effective ways to manage caching is by configuring the Cache-Control header in your website’s .htaccess file. This header tells the browser how long to store the cached version of resources such as images, CSS, and JavaScript files before requesting new versions from the server.

In this tutorial, we will explain how to configure the Cache-Control header in the .htaccess file to optimize the caching of your website’s static resources.

Understanding the max-age Directive

What is max-age?

The max-age directive specifies how long (in seconds) a resource is considered fresh and can be served from the cache without needing to request it from the server again. The longer the duration, the less frequently the browser will request that resource, improving performance.

Here are some common values for max-age:

  • One minute: max-age=60
  • One hour: max-age=3600
  • One day: max-age=86400
  • One week: max-age=604800
  • One month: max-age=2628000
  • One year: max-age=31536000

In the following examples, we’ll set the cache duration to one year (31536000 seconds) for image files, CSS files, and JavaScript files. You can modify the max-age value depending on your caching needs.

Configuring Cache-Control in .htaccess

To configure caching for your static resources, open your .htaccess file located in the root directory of your WordPress or website installation. Add the following rules to set the Cache-Control header with a one-year expiration time for images, CSS, and JavaScript files.

Code to Set Cache-Control for Static Resources:

HTML
# Set One year for image files
<filesMatch ".(jpg|jpeg|png|gif|ico)$">
Header set Cache-Control "max-age=31536000, public"
</filesMatch>
# Set One year for CSS and Javascript
<filesMatch ".(css|js)$">
Header set Cache-Control "max-age=31536000, public"
</filesMatch>
Explanation of the Code
  • filesMatch: This directive is used to match specific file types. In the first block, we target image file types (e.g., JPG, PNG, GIF, ICO), and in the second block, we target CSS and JavaScript files.
  • Header set Cache-Control: This command sets the Cache-Control header for the matched files. The max-age=31536000 part specifies that the browser should cache these resources for one year (31536000 seconds).
  • public: This directive means the resource is cacheable by both the browser and any intermediate caches (such as CDNs or proxy servers).
Modifying Cache Durations

Depending on your website’s needs, you can adjust the max-age value to control how long browsers cache different types of files. Here are some typical values you can use:

  • One day: max-age=86400
  • One week: max-age=604800
  • One month: max-age=2628000

If you frequently update your assets, consider setting shorter cache durations or using a cache-busting technique such as appending version numbers to your file names (e.g., style.css?v=1.0).

Conclusion

Configuring cache control in your .htaccess file is an easy and effective way to improve the performance of your website by reducing the number of requests made to the server. By setting long cache durations for static resources like images, CSS, and JavaScript, you allow the browser to reuse these files, leading to faster load times and improved user experience. However, remember to adjust your cache settings based on how often your resources are updated to avoid serving outdated content.

Next: Enable Gzip Compression via htaccess

Leave a Comment

Your email address will not be published. Required fields are marked *


Scroll to Top