How cache woff file with .htaccess - apache

I try to cache a .woff?v=4.6.3 file with .htaccess but this code doesn't works:
<ifModule mod_gzip.c>
mod_gzip_on Yes
mod_gzip_dechunk Yes
mod_gzip_item_include file .(html?|txt|css|js|php|pl)$
mod_gzip_item_include handler ^cgi-script$
mod_gzip_item_include mime ^text/.*
mod_gzip_item_exclude mime ^image/.*
mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
</ifModule>
<ifModule mod_expires.c>
ExpiresActive On
ExpiresDefault "access plus 2 days"
ExpiresByType image/jpeg "access plus 31104000 seconds"
ExpiresByType image/png "access plus 2592000 seconds"
ExpiresByType image/gif "access plus 2592000 seconds"
ExpiresByType text/css "access plus 1 year"
ExpiresByType text/javascript "access plus 216000 seconds"
ExpiresByType application/javascript "access plus 216000 seconds"
ExpiresByType text/html "access plus 3600 seconds"
ExpiresByType application/xhtml+xml "access plus 3600 seconds"
</ifModule>
Do you have any suggestion ? (NB: I search solution in google and on this forum but I don't find an applicable solution)

You can try using the filesmatch directive
<IfModule mod_headers.c>
<filesMatch ".(css|jpg|jpeg|png|gif|js|ico|woff)$">
Header set Cache-Control "max-age=2592000, public"
</filesMatch>
</IfModule>

Related

How do i cleanup my htaccess file?

A project i'm working on has a large htaccess file. But i'm sensing most of it is unneccary, yet i don't know how to use htaccess files at all.
How do i clean up this file?
Is there anything i should be aware of in this file?
Options -Indexes
ErrorDocument 404 /404
ErrorDocument 403 /404
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# Remove extra trailing slashes
RewriteCond %{REQUEST_URI} ^(.*)/{2,}(.*)$
RewriteRule . %1/%2 [R=301,L]
# Manage Uploads Directory
RewriteRule /(uploads/.*) $1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>
<ifModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/css application/x-javascript text/x-component text/html text/richtext image/svg+xml text/plain text/xsd text/xsl text/xml image/x-icon application/javascript
<filesmatch "\.(js|css|html|jpg|png|gif|eot|woff|ttf|svg)$">
SetOutputFilter DEFLATE
</filesmatch>
</ifModule>
<FilesMatch "\.(htaccess|htpasswd|ini|log|sh|inc|bak|tpl)$">
Order Allow,Deny
Deny from all
</FilesMatch>
<IfModule mod_expires.c>
ExpiresActive on
ExpiresDefault "access plus 1 week"
# CSS
ExpiresByType text/css "access plus 1 week"
# Data interchange
ExpiresByType application/json "access plus 0 seconds"
ExpiresByType application/ld+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/x-icon "access plus 1 week"
# HTML components (HTCs)
ExpiresByType text/x-component "access plus 1 week"
# HTML
ExpiresByType text/html "access plus 30 seconds"
# JavaScript
ExpiresByType application/javascript "access plus 1 week"
# 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
ExpiresByType audio/ogg "access plus 1 week"
ExpiresByType image/gif "access plus 1 week"
ExpiresByType image/jpeg "access plus 1 week"
ExpiresByType image/png "access plus 1 week"
ExpiresByType video/mp4 "access plus 1 week"
ExpiresByType video/ogg "access plus 1 week"
ExpiresByType video/webm "access plus 1 week"
# Web feeds
ExpiresByType application/atom+xml "access plus 1 hour"
ExpiresByType application/rss+xml "access plus 1 hour"
# Web fonts
ExpiresByType application/font-woff "access plus 1 week"
ExpiresByType application/font-woff2 "access plus 1 week"
ExpiresByType application/vnd.ms-fontobject "access plus 1 week"
ExpiresByType application/x-font-ttf "access plus 1 week"
ExpiresByType font/opentype "access plus 1 week"
ExpiresByType image/svg+xml "access plus 1 week"
</IfModule>
You need to know what you actually want, and then read the documentation for htaccess files and the various Apache modules.

How do I leverage browser caching of .woff fonts?

In PageSpeed Insights I keep seeing the message to leverage browser caching of a particular iconset/font I'm using: iconFont.woff (2 days)
I've set my .htaccess as so:
## EXPIRES CACHING ##
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType font/ttf "access 1 week"
ExpiresByType font/woff "access 1 week"
ExpiresByType image/jpg "access 1 month"
ExpiresByType image/jpeg "access 1 month"
ExpiresByType image/gif "access 1 month"
ExpiresByType image/png "access 1 month"
ExpiresByType text/css "access 1 week"
ExpiresByType application/pdf "access 1 month"
ExpiresByType application/javascript "access 1 month"
ExpiresByType application/x-shockwave-flash "access 1 month"
ExpiresByType image/x-icon "access 1 year"
ExpiresDefault "access 2 days"
</IfModule>
## EXPIRES CACHING ##
Despite this I'm still getting the same message in PageSpeed Insights. How do I cache this properly?
This is doing the job for me, as Google page speed is no longer asking to fix it. The AddType is essential.
# Fonts
# 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
# only uncomment if you dont have compression turned on already. Otherwise it will cause all other filestypes not to get compressed
# AddOutputFilterByType DEFLATE application/x-font-ttf application/x-font-opentype image/svg+xml
ExpiresActive on
# 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 application/font-woff2 "access plus 1 month"
ExpiresByType image/svg+xml "access plus 1 year"
With the help of Seb's IT blog this worked for me:
<IfModule mod_expires.c>
# Activate mod
ExpiresActive on
# Declare fonts content-type
AddType application/x-font-woff2 .woff2
# Set cache duration
ExpiresByType application/x-font-woff2 "access plus 1 month"
# Append "public" to header "Cache-Control"
<IfModule mod_headers.c>
Header append Cache-Control "public"
</IfModule>
</IfModule>

Set an ETag on all images

I'm trying to set an ETag on all images served through apache, per Google's recommendation here: https://developers.google.com/speed/docs/best-practices/caching. Here's my .htaccess:
<ifModule mod_expires.c>
ExpiresActive On
ExpiresDefault "access plus 5 seconds"
ExpiresByType image/x-icon "access plus 2592000 seconds"
ExpiresByType image/jpeg "access plus 2592000 seconds"
ExpiresByType image/png "access plus 2592000 seconds"
ExpiresByType image/gif "access plus 2592000 seconds"
ExpiresByType application/x-shockwave-flash "access plus 2592000 seconds"
</ifModule>
<IfModule mod_headers.c>
<filesMatch "\.(ico|jpe?g|png|gif|swf)$">
Header set Cache-Control "public"
FileETag MTime Size
</filesMatch>
</IfModule>
For some reason, that's not working. The HTTP headers on images have cache-control set but not an ETag. The website where I'm working this is: http://bestnovapainters.com/
Some other things running on the site (might be helpful to know): Wordpress 3.8.1, PHP 5.4.1, Clouflare CDN.
Thanks!

XAMPP loaded modules cause 500 in .htaccess

Why I get error 500? It's caused by .htaccess file
RewriteEngine On
RewriteBase /
AddDefaultCharset UTF-8
<IfModule mod_expires.c>
Header set cache-control: public
ExpiresActive on
ExpiresDefault "access plus 1 month"
ExpiresByType text/html "access plus 1 days"
ExpiresByType application/rss+xml "access plus 1 week"
ExpiresByType image/png "access plus 10 month"
ExpiresByType image/jpg "access plus 10 month"
ExpiresByType image/jpeg "access plus 10 month"
ExpiresByType image/gif "access plus 10 month"
ExpiresByType video/ogg "access plus 10 month"
ExpiresByType audio/ogg "access plus 10 month"
ExpiresByType video/mp4 "access plus 10 month"
ExpiresByType image/x-icon "access plus 10 month"
ExpiresByType font/ttf "access plus 10 month"
ExpiresByType image/svg+xml "access plus 10 month"
</IfModule>
<IfModule mod_headers.c>
<FilesMatch "\.(js|css|xml|gz)$">
Header append Vary: Accept-Encoding
</FilesMatch>
</IfModule>
Removing both sections
<IfModule mod_headers.c>
<IfModule mod_expires.c>
resolves problem.
I use XAMPP and those modules are present in \xampp\apache\modules:
mod_expires.so
mod_headers.so
Both are loaded with \xampp\apache\conf\httpd.conf:
LoadModule expires_module modules/mod_expires.so
LoadModule headers_module modules/mod_headers.so
Server was restarted.

correct htaccess caching parameters

I have been reading up on the subject of htaccess caching to help increase the speed of my website. I put together the following based on the information from different website sources as I want to improve the speed of my cms.
Can someone tell me if this is the proper way to use the parameters?
### caching rules
<IfModule mod_mime.c>
AddType application/x-javascript .js
AddType text/css .css
</IfModule>
<IfModule mod_deflate.c>
#AddOutputFilterByType DEFLATE text/text text/html text/plain text/xml text/css application/x-javascript application/javascript
AddOutputFilterByType DEFLATE text/css application/x-javascript text/x-component text/html text/richtext image/svg+xml text/plain text/xsd text/xsl text/xml image/x-icon application/javascript
<IfModule mod_setenvif.c>
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
</IfModule>
<IfModule mod_headers.c>
Header append Vary User-Agent env=!dont-vary
</IfModule>
</IfModule>
<IfModule mod_deflate.c>
SetOutputFilter DEFLATE
<IfModule mod_setenvif.c>
SetEnvIfNoCase Request_URI \.(?:rar|zip)$ no-gzip dont-vary
SetEnvIfNoCase Request_URI \.(?:gif|jpg|png)$ no-gzip dont-vary
SetEnvIfNoCase Request_URI \.(?:avi|mov|mp4)$ no-gzip dont-vary
SetEnvIfNoCase Request_URI \.mp3$ no-gzip dont-vary
</IfModule>
</IfModule>
<IfModule mod_gzip.c>
mod_gzip_on Yes
mod_gzip_dechunk Yes
mod_gzip_item_include file \.(html?|txt|css|js|php|pl)$
mod_gzip_item_include handler ^cgi-script$
mod_gzip_item_include mime ^text/.*
mod_gzip_item_include mime ^application/x-javascript.*
mod_gzip_item_exclude mime ^image/.*
mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
</IfModule>
Header unset Pragma
FileETag None
Header unset ETag
<IfModule mod_headers.c>
Header unset ETag
</IfModule>
FileETag None
<FilesMatch "\.(ico|jpg|jpeg|png|gif|js|css|swf|pdf|flv|mp3)$">
<IfModule mod_expires.c>
ExpiresActive on
ExpiresDefault "access plus 14 days"
Header set Cache-Control "public"
</IfModule>
</FilesMatch>
<FilesMatch "\.(html|htm|xml|txt|xsl)$">
Header set Cache-Control "max-age=7200, must-revalidate"
</FilesMatch>
<IfModule mod_expires.c>
ExpiresActive On
#ExpiresDefault "access plus 1 week"
ExpiresDefault "access plus 86400 seconds"
ExpiresByType image/x-icon "modification plus 1 year"
ExpiresByType image/gif "access plus 1 year"
ExpiresByType image/png "access plus 1 year"
ExpiresByType image/jpeg "access plus 1 year"
ExpiresByType image/jpg "access plus 1 year"
ExpiresByType text/js "access plus 1 month"
ExpiresByType text/javascript "access plus 1 month"
ExpiresByType application/javascript "access plus 1 month"
ExpiresByType application/x-javascript "access plus 1 month"
ExpiresByType application/x-shockwave-flash "access plus 1 month"
ExpiresByType video/x-flv "access plus 1 month"
ExpiresByType application/pdf "access plus 1 month"
#ExpiresByType text/html "access plus 1 day"
#ExpiresByType text/css "access plus 1 day"
#ExpiresByType application/xhtml+xml "access plus 1 day"
#ExpiresByType text/plain "access plus 1 month"
#ExpiresByType text/javascript "access plus 604800 seconds"
#ExpiresByType application/javascript "access plus 604800 seconds"
</IfModule>
The following line needs updating from:
SetEnvIfNoCase Request_URI \.(?:gif|jpg|png)$ no-gzip dont-vary
to
SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png)$ no-gzip dont-vary
You also seem to have some duplication here:
Header unset Pragma
FileETag None
Header unset ETag
<IfModule mod_headers.c>
Header unset ETag
</IfModule>
FileETag None
And additionally, it'd be worth combining your <IfModule> directives, so all the directives for mod_expires are in one location and you should also remove the duplicate expires settings. E.g.
<FilesMatch "\.(ico|jpg|jpeg|png|gif|js|css|swf|pdf|flv|mp3)$">
<IfModule mod_expires.c>
ExpiresActive on
ExpiresDefault "access plus 14 days"
Header set Cache-Control "public"
</IfModule>
</FilesMatch>
gets overwritten by
<IfModule mod_expires.c>
ExpiresActive On
ExpiresDefault "access plus 86400 seconds"
ExpiresByType image/x-icon "modification plus 1 year"
ExpiresByType image/gif "access plus 1 year"
ExpiresByType image/png "access plus 1 year"
ExpiresByType image/jpeg "access plus 1 year"
ExpiresByType image/jpg "access plus 1 year"
ExpiresByType text/js "access plus 1 month"
ExpiresByType text/javascript "access plus 1 month"
Other than that, yes.