Caching enables the storage of frequently accessed data temporarily, reducing server load and enhancing user experience. In this guide, we will explore how to leverage .htaccess to set up caching on a Linux server.

What is .htaccess?

.htaccess (hypertext access) is a configuration file utilized by Apache web servers to enable decentralized management of server configuration. It allows for directory-level configuration changes and is commonly used to control access, set up redirects, and implement various other functionalities.

htaccess

Setting Up Cache with .htaccess

To enable caching via .htaccess, follow these steps:

Step 1: Access .htaccess File

Using an FTP client or a file manager provided by your hosting provider, navigate to the root directory of your website. Look for the .htaccess file. If it doesn’t exist, you can create one.

Step 2: Enable Mod_Expires

Ensure that mod_expires is enabled on your server. This module provides the ability to set expiration dates for different types of files. You can enable it by adding the following line to your Apache configuration:


LoadModule expires_module modules/mod_expires.so

 

Step 3: Configure Cache-Control Headers

Add the following lines to your .htaccess file to define the caching duration for various file types:

 

  # Enable Expires headers

  ExpiresActive On

 

  # Set expiration time for images to 1 year (adjust as needed)

  ExpiresByType image/jpeg "access plus 1 year"

  ExpiresByType image/png "access plus 1 year"

  ExpiresByType image/gif "access plus 1 year"

  ExpiresByType image/svg+xml "access plus 1 year"

 

  # Set expiration time for CSS and JavaScript to 1 week

  ExpiresByType text/css "access plus 1 week"

  ExpiresByType application/javascript "access plus 1 week"

 

  # Set expiration time for HTML and XML to 1 day

  ExpiresByType text/html "access plus 1 day"

  ExpiresByType text/xml "access plus 1 day"

 


Adjust the expiration times according to your specific requirements.

 

Step 4: Implement ETag

ETag (entity tag) is an HTTP response header that helps in validating cache. Add the following lines to your .htaccess file to implement ETag:



# Enable ETag
Header unset ETag

FileETag None
 

Step 5: Verify Configuration

After making the changes, save the .htaccess file and upload it to your server. Verify the configuration by accessing your website and checking the response headers using browser developer tools or online tools like GTmetrix or Pingdom.

<IfModule mod_headers.c>
Header set Connection keep-alive

# Cache-control headers
    # 2 HOURS
    #<filesMatch "*">
        Header set Cache-Control "max-age=7200, must-revalidate"
    #</filesMatch>

    # 480 weeks - 290304000
    # 2 WEEKS
    <filesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|js|swf)$">
        Header set Cache-Control "max-age=1209600, public"
    </filesMatch>

    # 1 DAY
    <filesMatch "\.(css)$">
        Header set Cache-Control "max-age=86400, public, must-revalidate"
        #Header set Cache-Control "max-age=0, public, must-revalidate"
    </filesMatch>

    # 2 DAYS
    <filesMatch "\.(xml|txt)$">
        Header set Cache-Control "max-age=172800, public, must-revalidate"
    </filesMatch>

    # 2 HOURS
    <filesMatch "\.(html|htm)$">
        Header set Cache-Control "max-age=7200, must-revalidate"
    </filesMatch>

    <FilesMatch "\.(gif|jpg|png|ico|css|js|pdf|txt)$">
        Header append Cache-Control "public"
    </FilesMatch>
</IfModule>


By configuring caching using .htaccess, you can significantly improve the performance of your website by reducing server load and speeding up page load times for your visitors. Remember to test your configuration thoroughly and adjust cache durations based on your website’s content and update frequency. Implementing caching is a crucial step towards optimizing your website for better user experience and search engine rankings

Published: 09 May 2024 02:07