26

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
  • 19,449
  • 54
  • 157
  • 290
  • 1
    @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!? :) – Brett Feb 17 '18 at 11:10
  • 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. – jww Feb 17 '18 at 11:18
  • 3
    @jww `.htaccess` IS on topic. See [this](https://meta.stackoverflow.com/questions/283033/are-htaccess-questions-ever-on-topic-at-so) FAQ article. – Papershine Feb 17 '18 at 11:19
  • 1
    @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. – jww Feb 17 '18 at 11:24
  • 3
    @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 Feb 17 '18 at 16:48

2 Answers2

77

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"
John
  • 1
  • 13
  • 98
  • 177
Brett
  • 19,449
  • 54
  • 157
  • 290
  • 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? – Ariel Allon Oct 16 '15 at 18:55
  • 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. :) – Brett Oct 17 '15 at 05:41
  • 1
    `AddType application/font-woff2 .woff2` and `ExpiresByType application/font-woff2 "access plus 6 month"` for the recommended woff2 –  Jun 02 '18 at 16:16
  • @BO41 Thanks for that, but any idea why this is `font-woff2` and not `x-font-woff2` like the others? – Brett Jun 03 '18 at 18:09
  • @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 (https://stackoverflow.com/questions/3594823/mime-type-for-woff-fonts) –  Jun 04 '18 at 13:43
  • @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. – Brett Jun 06 '18 at 19:55
2

I believe your issue is how you define the fonts, and there is no need to add their content-type

 ExpiresByType font/truetype "access plus 1 year"
 ExpiresByType font/opentype "access plus 1 year"
 ExpiresByType application/x-font-woff   "access plus 1 year"
 ExpiresByType image/svg+xml "access plus 1 year"
 ExpiresByType application/vnd.ms-fontobject "access plus 1 year"
Saddam Abu Ghaida
  • 6,381
  • 2
  • 22
  • 29