Setting expire headers for fonts not working

52,262

Seems I had to include this bit as well:

ExpiresActive on

With the full code being:

# Add correct content-type for fonts
AddType application/vnd.ms-fontobject .eot
AddType font/ttf .ttf
AddType font/otf .otf
AddType font/woff .woff
AddType font/woff2 .woff2
AddType image/svg+xml .svg

# Compress compressible fonts
AddOutputFilterByType DEFLATE font/ttf font/otf image/svg+xml

ExpiresActive on

# Add a far future Expires header for fonts
ExpiresByType application/vnd.ms-fontobject "access plus 1 year"
ExpiresByType font/ttf "access plus 1 year"
ExpiresByType font/otf "access plus 1 year"
ExpiresByType font/woff "access plus 1 year"
ExpiresByType font/woff2 "access plus 1 year"
ExpiresByType image/svg+xml "access plus 1 year"
Share:
52,262

Related videos on Youtube

Brett
Author by

Brett

Updated on July 13, 2020

Comments

  • Brett
    Brett almost 4 years

    I am using the below code in my .htaccess file to try and set the expire headers for some fonts, but upon checking my firefox cache and the expire header, the font is set to expire in about 12 hours from now; not the 1 year I am trying to set it to.

    Here is my code:

    # Add correct content-type for fonts
    AddType application/vnd.ms-fontobject .eot
    AddType application/x-font-ttf .ttf
    AddType application/x-font-opentype .otf
    AddType application/x-font-woff .woff
    AddType image/svg+xml .svg
    
    # Compress compressible fonts
    AddOutputFilterByType DEFLATE application/x-font-ttf application/x-font-opentype image/svg+xml
    
    # Add a far future Expires header for fonts
    ExpiresByType application/vnd.ms-fontobject "access plus 1 year"
    ExpiresByType application/x-font-ttf "access plus 1 year"
    ExpiresByType application/x-font-opentype "access plus 1 year"
    ExpiresByType application/x-font-woff "access plus 1 year"
    ExpiresByType image/svg+xml "access plus 1 year"
    

    What am I doing wrong?

    • Brett
      Brett about 6 years
      @jww Was this really a necessary comment after 5+ years? If you're so concerned about "on-topic" questions perhaps you should go and troll newer questions instead!? :)
    • jww
      jww about 6 years
      The problem is, people see this question and then ask similar questions. The only way I know to signal off-topic-ness that persists over time is the custom close message.
    • Papershine
      Papershine about 6 years
      @jww .htaccess IS on topic. See this FAQ article.
    • jww
      jww about 6 years
      @paper1111 - For a question to be on-topic here, it must satisfy both "software tools commonly used by programmers" AND "practical, answerable problem that is unique to software development". Apache config files certainly don't satisfy the later. Anything else is just wishful thinking.
    • Brett
      Brett about 6 years
      @jww I'm with paper1111 on this one. According to that meta question it is fine. Additionally, this is why a lot of people get ticked off with some of the people @ SO - because there are so many anal people that get overzealous in screaming something is off-topic or a duplicate.
  • Brett
    Brett over 11 years
    I will try that, but I got that definition from here: stackoverflow.com/questions/2871655/proper-mime-type-for-fon‌​ts
  • Brett
    Brett over 11 years
    Ok, just tested this out and it made no change. :(
  • Ariel Allon
    Ariel Allon over 8 years
    That worked for me as well. What's weird, though, is that I simply had an ExpiresDefault "access plus 1 month" that was handling all file types (html, jpg, css, js, etc) correctly except svg. Once I added ExpiresActive on, the ExpiresDefault also worked for svg. Any ideas why that would be?
  • Brett
    Brett over 8 years
    Hard to say why it worked for you without ExpiresActive on, but just be sure to always use it in the future to be certain. :)
  • Admin
    Admin almost 6 years
    AddType application/font-woff2 .woff2 and ExpiresByType application/font-woff2 "access plus 6 month" for the recommended woff2
  • Brett
    Brett almost 6 years
    @BO41 Thanks for that, but any idea why this is font-woff2 and not x-font-woff2 like the others?
  • Admin
    Admin almost 6 years
    @Brett no sorry. I don't know. maybe the others work without the "x-" as well or it is something browser specific. Didn't find much online. I just know that it works like this in up-to-date chromium and firefox. maybe you find something online (stackoverflow.com/questions/3594823/mime-type-for-woff-font‌​s)
  • Brett
    Brett almost 6 years
    @BO41 Thanks. In my research I found out that the mime-types apparently changed for .ttf and .otf, however I haven't tested them yet so I'm weary of updating my solution until tested.