1

I have added expires headers to my .htaccess file, they don't really seem to work though.

When checking the result in both Firefox and Chrome (by pressing F12, chosing the Network option and hitting F5 to reload the page), I notice that the rules that I have added in the file are being ignored.

These are the rules, which have been copied from boilerplate:

# ----------------------------------------------------------------------
# | ETags                                                              |
# ----------------------------------------------------------------------

# Remove `ETags` as resources are sent with far-future expires headers.
#
# https://developer.yahoo.com/performance/rules.html#etags
# https://tools.ietf.org/html/rfc7232#section-2.3

# `FileETag None` doesn't work in all cases.
<IfModule mod_headers.c>
    Header unset ETag
</IfModule>

FileETag None

# ----------------------------------------------------------------------
# | Expires headers                                                    |
# ----------------------------------------------------------------------

# Serve resources with far-future expires headers.
#
# (!) If you don't control versioning with filename-based
# cache busting, you should consider lowering the cache times
# to something like one week.
#
# https://httpd.apache.org/docs/current/mod/mod_expires.html

<IfModule mod_expires.c>

    ExpiresActive on
    ExpiresDefault                                      "access plus 1 month"

  # CSS

    ExpiresByType text/css                              "access plus 1 year"


  # Data interchange

    ExpiresByType application/atom+xml                  "access plus 1 hour"
    ExpiresByType application/rdf+xml                   "access plus 1 hour"
    ExpiresByType application/rss+xml                   "access plus 1 hour"

    ExpiresByType application/json                      "access plus 0 seconds"
    ExpiresByType application/ld+json                   "access plus 0 seconds"
    ExpiresByType application/schema+json               "access plus 0 seconds"
    ExpiresByType application/vnd.geo+json              "access plus 0 seconds"
    ExpiresByType application/xml                       "access plus 0 seconds"
    ExpiresByType text/xml                              "access plus 0 seconds"


  # Favicon (cannot be renamed!) and cursor images

    ExpiresByType image/vnd.microsoft.icon              "access plus 1 week"
    ExpiresByType image/x-icon                          "access plus 1 week"

  # HTML

    ExpiresByType text/html                             "access plus 0 seconds"


  # JavaScript

    ExpiresByType application/javascript                "access plus 1 year"
    ExpiresByType application/x-javascript              "access plus 1 year"
    ExpiresByType text/javascript                       "access plus 1 year"


  # Manifest files

    ExpiresByType application/manifest+json             "access plus 1 week"
    ExpiresByType application/x-web-app-manifest+json   "access plus 0 seconds"
    ExpiresByType text/cache-manifest                   "access plus 0 seconds"


  # Media files

    ExpiresByType audio/ogg                             "access plus 1 month"
    ExpiresByType image/bmp                             "access plus 1 month"
    ExpiresByType image/gif                             "access plus 1 year"
    ExpiresByType image/jpeg                            "access plus 1 year"
    ExpiresByType image/png                             "access plus 1 year"
    ExpiresByType image/svg+xml                         "access plus 1 month"
    ExpiresByType image/webp                            "access plus 1 month"
    ExpiresByType video/mp4                             "access plus 1 month"
    ExpiresByType video/ogg                             "access plus 1 month"
    ExpiresByType video/webm                            "access plus 1 month"


  # Web fonts

    # Embedded OpenType (EOT)
    ExpiresByType application/vnd.ms-fontobject         "access plus 1 month"
    ExpiresByType font/eot                              "access plus 1 month"

    # OpenType
    ExpiresByType font/opentype                         "access plus 1 month"

    # TrueType
    ExpiresByType application/x-font-ttf                "access plus 1 month"

    # Web Open Font Format (WOFF) 1.0
    ExpiresByType application/font-woff                 "access plus 1 month"
    ExpiresByType application/x-font-woff               "access plus 1 month"
    ExpiresByType font/woff                             "access plus 1 month"

    # Web Open Font Format (WOFF) 2.0
    ExpiresByType application/font-woff2                "access plus 1 month"


  # Other

    ExpiresByType text/x-cross-domain-policy            "access plus 1 week"

</IfModule>

And these are the headers shown in the browser. As you can see, components such as scripts and stylesheets expire after one month, while images expire after two months.

Stylesheet headers:

Cache-Control: max-age=2592000, public
Content-Encoding: gzip
Content-Type: text/css
Date: Tue, 21 Jun 2016 16:06:22 GMT
Expires: Thu, 21 Jul 2016 16:06:22 GMT

Script headers:

Cache-Control: max-age=2592000, public
Content-Encoding: gzip
Content-Type: application/javascript
Date: Tue, 21 Jun 2016 16:06:22 GMT
Expires: Thu, 21 Jul 2016 16:06:22 GMT

Image headers:

Cache-Control: max-age=5184000, public
Connection: keep-alive
Date: Tue, 21 Jun 2016 16:10:13 GMT
Expires: Sat, 20 Aug 2016 16:10:13 GMT

Is there something that I can do about this?

  • 1
    " I notice that the rules that I have added in the file are being ignored." Could you elaborate a bit more on this part please? How did you notice it? – Florian Lemaitre Jun 21 '16 at 15:39
  • @FlorianLemaitre You are right, I have added some more information on this. Thank you. –  Jun 21 '16 at 16:13

1 Answers1

0

First of all, confirm with curl -IL "(URL)" for the exact headers the web server is sending.

Second, ensure you don't have the "Ignore cache" checkbox selected in Chrome.

Third, what does the access log say about these requests, when doing it from curl, Chrome and Firefox?

Edit: the actual answer was to check if the expires module was actually enabled.

Nico Andrade
  • 880
  • 5
  • 16
  • 1. The command returned the following information: Last-Modified: Mon, 20 Jun 2016 18:08:38 GMT. Cache-Control: max-age=0. Expires: Tue, 21 Jun 2016 18:54:03 GMT. 2. No, I haven't checked it.. 3. I used curl -v, which returned this: Date: Tue, 21 Jun 2016 19:12:34 GMT. Connection: keep-alive. Last-Modified: Mon, 20 Jun 2016 18:08:38 GMT. Cache-Control: max-age=0. Expires: Tue, 21 Jun 2016 19:12:34 GMT. The addon HTTP Request Logger on Firefox only showed the files that were downloaded when I visited the site. Could you tell me if there is some more useful way to view the logs? –  Jun 21 '16 at 19:32
  • 1
    So I think you don't have the mod_expires.c module installed or enabled. Ensure you have enabled the module mod_expires.c , as explained here http://www.absolutelytech.com/2010/08/02/howto-enable-or-disable-modules-in-apache/ – Nico Andrade Jun 21 '16 at 19:37
  • My hosting provider is supposed to have enabled it by default, they said they will check it out for sure though. So I guess I will have to wait for a response. In the meantime, thank you very much. –  Jun 22 '16 at 07:57
  • 1
    You can check it yourself. Just create a `phpinfo.php` file containing ` – Florian Lemaitre Jun 22 '16 at 08:05
  • @FlorianLemaitre I have already got their response, but I will definitely use your suggestion the next time I need to check the modules that are enabled. Once again, thank you. –  Jun 22 '16 at 09:32