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

What are Template literals (Template strings) in JS?

How to block IPv6 IP addresses in .htaccess

How to select all rows for a certain month (or day, year or time), using Eloquent

How to get the last character of a string in JS?

What are macros in Laravel?

How to set custom HTTP headers in Apache (by editing .htaccess)

How to provide a default model object for relationships?

Force trailing slash (redirect if no trailing slash exists) in .htaccess

What is the max size, by default, of GET variables?

What is Bubble sort, and how to write an implementation of it in PHP