Skip to content

Adding Expires Header to htaccess

Expires Headers are a set of rules or directives from a website to tell a user’s local browser to either look in its own cache for files or to request the files off the webserver, the former is better for speed of the web page load and a reduction in the webserver processing.

For Apache webservers you add the directives in the .htaccess file.

<IfModule mod_expires.c>
  ExpiresActive On

  # Images
  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/svg+xml "access plus 1 year"
  ExpiresByType image/x-icon "access plus 1 year"

  # Video
  ExpiresByType video/mp4 "access plus 1 year"
  ExpiresByType video/mpeg "access plus 1 year"

  # CSS, JavaScript
  ExpiresByType text/css "access plus 1 month"
  ExpiresByType text/javascript "access plus 1 month"
  ExpiresByType application/javascript "access plus 1 month"

  # Others
  ExpiresByType application/pdf "access plus 1 month"
  ExpiresByType application/x-shockwave-flash "access plus 1 month"
  
  # Everything else
  ExpiresDefault "access 7 days"
 
</IfModule>


So in the file above certain file types are explicitly set with ‘ExpiresByType‘ with assets like images and videos set to be retained by a year and files like css and js files set to be retained by a month. Then all other files controlled by ‘ExpiresDefault‘ setting the cache time for 7 days this would include html/php files.

You can adjust these times to your preference.

Ref

Leave a Comment





Scroll To Top