The cart is empty

Content caching is one of the most effective ways to improve the performance and loading speed of websites. By setting caching rules in the .htaccess file, you can control how long browsers store different types of files in their cache. This article provides a detailed guide on how to set content caching rules using the ExpiresActive On and ExpiresDefault A600 directives.

What is Content Caching?

Content caching allows web browsers to store copies of files (such as images, CSS, and JavaScript) on the client side. This means these files do not need to be downloaded again on each visit to the website, significantly speeding up page load times and reducing server load.

Prerequisites

Before setting up content caching rules, ensure you have access to the .htaccess file on your web server. This file is typically located in the root directory of your web application.

Basic Caching Setup with ExpiresActive and ExpiresDefault

Open the .htaccess file in your text editor and add the following code:

<IfModule mod_expires.c>
  ExpiresActive On
  ExpiresDefault A600
</IfModule>

Code Explanation

  • <IfModule mod_expires.c>: Checks if the mod_expires module is loaded.
  • ExpiresActive On: Activates the Expires function.
  • ExpiresDefault A600: Sets the default caching duration for all file types to 600 seconds (10 minutes).

Advanced Caching Setup

For better control over the caching of different file types, you can specify rules for each content type. The following example shows how to set different caching durations for images, CSS, JavaScript, and HTML files:

<IfModule mod_expires.c>
  ExpiresActive On

  # Default caching duration (10 minutes)
  ExpiresDefault A600

  # Images
  ExpiresByType image/jpg A2592000
  ExpiresByType image/jpeg A2592000
  ExpiresByType image/png A2592000
  ExpiresByType image/gif A2592000
  ExpiresByType image/webp A2592000

  # CSS and JavaScript
  ExpiresByType text/css A604800
  ExpiresByType application/javascript A604800
  ExpiresByType application/x-javascript A604800

  # HTML
  ExpiresByType text/html A600
</IfModule>

Code Explanation

  • ExpiresByType image/jpg A2592000: Sets the caching duration for JPEG images to 2592000 seconds (30 days).
  • ExpiresByType text/css A604800: Sets the caching duration for CSS files to 604800 seconds (7 days).
  • ExpiresByType text/html A600: Sets the caching duration for HTML files to 600 seconds (10 minutes).

 

Setting content caching rules in the .htaccess file is an effective way to improve the performance of your website. This process involves editing the .htaccess file and adding the necessary directives to control the caching duration of different file types in the browser cache. Regular testing and optimization ensure that your pages remain fast and user-friendly.