How to set the Expires header in .htaccess

Apache: How to set the Expires header in .htaccess

Setting the expires header is a great way to enable browser based caching, which should help make some pages load faster. Here is the code how to do it in .htaccess.

  1. <IfModule mod_expires.c>
  2.         ExpiresActive on
  3.  
  4.         ExpiresDefault                                    "access plus 1 month"
  5.  
  6.  
  7.     # TEXT STUFF
  8.        ExpiresByType text/css                            "access plus 1 year"
  9.         ExpiresByType application/json                    "access plus 0 seconds"
  10.         ExpiresByType application/xml                     "access plus 0 seconds"
  11.         ExpiresByType text/xml                            "access plus 0 seconds"
  12.  
  13.     # FAVICON
  14.        ExpiresByType image/x-icon                        "access plus 1 week"
  15.  
  16.     # HTML
  17.        ExpiresByType text/html                           "access plus 0 seconds"
  18.  
  19.     # JAVASCRIPT
  20.        ExpiresByType application/javascript              "access plus 1 year"
  21.  
  22.     # FONTS
  23.        ExpiresByType application/font-woff2              "access plus 1 month"
  24.         ExpiresByType application/font-woff               "access plus 1 month"
  25.         ExpiresByType application/vnd.ms-fontobject       "access plus 1 month"
  26.         ExpiresByType application/x-font-ttf              "access plus 1 month"
  27.         ExpiresByType font/opentype                       "access plus 1 month"
  28.         ExpiresByType image/svg+xml                       "access plus 1 month"
  29.  
  30.  
  31.         ExpiresByType application/x-web-app-manifest+json "access plus 0 seconds"
  32.         ExpiresByType text/cache-manifest                 "access plus 0 seconds"
  33.  
  34.         ExpiresByType audio/ogg                           "access plus 1 month"
  35.         ExpiresByType image/gif                           "access plus 1 month"
  36.         ExpiresByType image/jpeg                          "access plus 1 month"
  37.         ExpiresByType image/png                           "access plus 1 month"
  38.         ExpiresByType video/mp4                           "access plus 1 month"
  39.         ExpiresByType video/ogg                           "access plus 1 month"
  40.         ExpiresByType video/webm                          "access plus 1 month"
  41.  
  42.         ExpiresByType application/atom+xml                "access plus 1 hour"
  43.         ExpiresByType application/rss+xml                 "access plus 1 hour"
  44.  
  45. </IfModule>

webdevetc profile pic
webdevetc

I am a 29 year old backend web developer from London, mostly focusing on PHP and Laravel lately. This (webdevetc.com) is my blog where I write about some web development topics (PHP, Laravel, Javascript, and some server stuff). contact me here.



More...


Comments and discussion about How to set the Expires header in .htaccess

Found this interesting? Maybe you want to read some more in this series?

Or see other topics in the Apache language

Or see other languages/frameworks:
PHP Laravel Composer Apache CentOS and Linux Stuff WordPress General Webdev and Programming Stuff JavaScript
Or see random questions

How to use subdomains as parameters in your routes (or in your web.php file)

How to have a whitelist of IP addresses that can access files in .htaccess

How to check if a string contains a certain substring?

What are Apache MPMs?

How to set the timezone to UTC in .htaccess

301 Redirect a single URL in .htaccess to a new location

How to create random numbers in JS?

What are generators in PHP, and how do they compare to arrays?

How to check if a user is logged in, inside a blade file?

How to fix the MySQL related "Specified key was too long error" error in Laravel