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 convert between types in JS?

How to Enable Directory Listings in Apache with .htaccess

How to force non www in htaccess

How to set up custom error pages in .htaccess for your Apache server (ErrorDocument)

How can you check if a variable is alphanum (alphanumeric, letters and numbers only) in PHP?

What are some ways you can log debug messages and data in JS?

What is the loop variable in a @foreach loop in Blade?

How to find the index of a value in an array in JS?

How to block access to certain referrers in Apache using .htaccess

How to install and use jpegoptim on Linux CentOS