Setting expire headers for fonts not working
Asked Answered
D

2

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?

Delamare answered 17/11, 2012 at 10:6 Comment(5)
@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!? :)Delamare
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.Hotblooded
@Hotblooded .htaccess IS on topic. See this FAQ article.Nonlegal
@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.Hotblooded
@Hotblooded 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.Delamare
D
78

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"
Delamare answered 17/11, 2012 at 11:5 Comment(6)
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?Havard
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. :)Delamare
AddType application/font-woff2 .woff2 and ExpiresByType application/font-woff2 "access plus 6 month" for the recommended woff2Aloft
@BO41 Thanks for that, but any idea why this is font-woff2 and not x-font-woff2 like the others?Delamare
@Delamare 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://mcmap.net/q/73174/-mime-type-for-woff-fonts)Aloft
@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.Delamare
P
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"
Ptolemaeus answered 17/11, 2012 at 10:15 Comment(2)
I will try that, but I got that definition from here: https://mcmap.net/q/75521/-proper-mime-type-for-otf-fontsDelamare
Ok, just tested this out and it made no change. :(Delamare

© 2022 - 2024 — McMap. All rights reserved.