2

I have read several guides for how to write the htaccess file for caching but with no success. I asume I wrote something wrong and need some help on finding what i did wrong. Here's my htaccess file:

Options +FollowSymlinks
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

# 1 YEAR - doesn't change often
<FilesMatch "\.(flv|ico|pdf|avi|mov|ppt|doc|mp3|wmv|wav)$">
Header set Cache-Control "proxy-revalidate, max-age=31536000"
</FilesMatch>

# 1 WEEK - possible to be changed, unlikely
<FilesMatch "\.(jpg|jpeg|png|gif|swf)$">
Header set Cache-Control "proxy-revalidate, max-age=604800"
</FilesMatch>

# 3 HOUR - core content, changes quickly
<FilesMatch "\.(txt|xml|js|css)$">
Header set Cache-Control "proxy-revalidate, max-age=10800"
</FilesMatch>

The non-www to www rewrite is working fine btw

  • Any cache over 4-12 weeks is kinda pointless. Most browsers will clear their cache before this. Most likely your host does not support the files match method. – Simon Hayter Aug 30 '15 at 19:11
  • well it doesn't harm :-) – Asaf David Aug 30 '15 at 19:19
  • The FilesMatch directive is a core directive - I'm not aware of hosts not supporting this? (Can hosts disable this, providing .htaccess files are allowed?) However, FilesMatch matches physical files on the filesystem, so if you are internally rewriting to files with a different extension then it's possible these directives won't match. (?) – MrWhite Sep 11 '15 at 21:52
2

Try using the mod_expires syntax using something like this:

<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access 2 week"
ExpiresByType image/jpeg "access 2 week"
ExpiresByType image/gif "access 2 week"
ExpiresByType image/png "access 2 week"
ExpiresByType text/css "access 2 week"
ExpiresByType text/html "access 1 hour"
ExpiresByType application/pdf "access 2 week"
ExpiresByType text/x-javascript "access 2 week"
ExpiresByType application/x-shockwave-flash "access 2 week"
ExpiresByType image/x-icon "access 1 month"
ExpiresDefault "access 2 week"
</IfModule>
|improve this answer|||||
  • Finally it works! I've been struggling with this for the last week+. Thanks a lot Simon! – Asaf David Aug 30 '15 at 19:26
  • I see text/x-javascript used a lot in this context, but shouldn't this be application/javascript for most modern servers? Was text/x-javascript ever in common usage? I can find very little on this supposed mime-type. The only place I've seen it used is in examples of ExpiresByType such as this? – MrWhite Oct 10 '15 at 21:29
  • Hey @W3d, yep your totally right, there seems to be some confusion what should be used in the htaccess. text/x-javascript was experimental and text/javascript now obsolete in MIME type. application/javascript supersede text/javascript, but, Apache seems to understand at least the ones in my code. I'll test the later code and update the answer if it works. – Simon Hayter Oct 11 '15 at 8:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.