How to setup cache validator and speed up website with Apache caching header

How to setup cache validator and speed up website Apache caching header

You can enable/set up cache validator from .htaccess.  Insert below code in your .htaccess file and restart Apache server.

 

=========================================

<IfModule mod_expires.c>
ExpiresActive on

# Perhaps better to whitelist expires rules? Perhaps.
ExpiresDefault                          “access plus 1 year”

# cache.appcache needs re-requests in FF 3.6 (thx Remy ~Introducing HTML5)
ExpiresByType text/cache-manifest       “access plus 1 month”

# Your document html
ExpiresByType text/html                 “access plus 1 month”

# Data
ExpiresByType text/xml                  “access plus 1 month”
ExpiresByType application/xml           “access plus 1 month”
ExpiresByType application/json          “access plus 1 month”

# RSS feed
ExpiresByType application/rss+xml       “access plus 1 month”

# Favicon (cannot be renamed)
ExpiresByType image/x-icon              “access plus 1 month”

# Media: images, video, audio
ExpiresByType image/gif                 “access plus 1 month”
ExpiresByType image/png                 “access plus 1 month”
ExpiresByType image/jpg                 “access plus 1 month”
ExpiresByType image/jpeg                “access plus 1 month”
ExpiresByType video/ogg                 “access plus 1 month”
ExpiresByType audio/ogg                 “access plus 1 month”
ExpiresByType video/mp4                 “access plus 1 month”
ExpiresByType video/webm                “access plus 1 month”

# HTC files  (css3pie)
ExpiresByType text/x-component          “access plus 1 month”

# Webfonts
ExpiresByType font/truetype             “access plus 1 month”
ExpiresByType font/opentype             “access plus 1 month”
ExpiresByType application/x-font-woff   “access plus 1 month”
ExpiresByType image/svg+xml             “access plus 1 month”
ExpiresByType application/vnd.ms-fontobject “access plus 1 month”

# CSS and JavaScript
ExpiresByType text/css                  “access plus 1 year”
ExpiresByType application/javascript    “access plus 1 year”
ExpiresByType text/javascript           “access plus 1 year”

<IfModule mod_headers.c>

##### DYNAMIC PAGES
<FilesMatch “\\.(ast|php|css|js)$”>
Header set Cache-Control “public, max-age=3600, must-revalidate”
</FilesMatch>

##### STATIC FILES
<FilesMatch “\\.(png|svg|swf|js|xml|css|js)$”>
Header set Cache-Control “public, max-age=604800, must-revalidate”
</FilesMatch>
</IfModule>

##### ETERNAL FILES
<FilesMatch “\.(flv|gif|jpg|jpeg|png|ico|swf|css|js|html|pdf)$”>
Header set Cache-Control “max-age=20082014, private, must-revalidate”
FileETag None
</FilesMatch>

====================================================

 

2 Comments to “How to setup cache validator and speed up website with Apache caching header”

  1. When i used this code, it was throwing serber error.

    • Hi Jyoti,

      You can use this code in .htaccess file. Please find below tested code.

      Also check expire module is enabled in Apache.


      ExpiresActive On
      ExpiresByType image/jpg “access plus 60 days”
      ExpiresByType image/png “access plus 60 days”
      ExpiresByType image/gif “access plus 60 days”
      ExpiresByType image/jpeg “access plus 60 days”
      ExpiresByType text/css “access plus 1 days”
      ExpiresByType image/x-icon “access plus 1 month”
      ExpiresByType application/pdf “access plus 1 month”
      ExpiresByType audio/x-wav “access plus 1 month”
      ExpiresByType audio/mpeg “access plus 1 month”
      ExpiresByType video/mpeg “access plus 1 month”
      ExpiresByType video/mp4 “access plus 1 month”
      ExpiresByType video/quicktime “access plus 1 month”
      ExpiresByType video/x-ms-wmv “access plus 1 month”
      ExpiresByType application/x-shockwave-flash “access 1 month”
      ExpiresByType text/javascript “access plus 1 week”
      ExpiresByType application/x-javascript “access plus 1 week”
      ExpiresByType application/javascript “access plus 1 week”

Leave a comment

Your email address will not be published.


*