Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

htaccess caching not working

I inputted the following into my ".htacces" file in order to start caching the Web Content. According to Google Page Speed and Yslow the pages are still not Cached. Are the modules wrong? Or is it that the apps are not showing the data correctly?

Site is running on Apache 2.0 on Server

.htaccess (part with caching modules):

# Expire headers
<ifModule mod_expires.c>
  ExpiresActive On
  ExpiresDefault "access plus 1 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"
  ExpiresByType text/css "access plus 604800 seconds"
  ExpiresByType text/javascript "access plus 216000 seconds"
  ExpiresByType application/javascript "access plus 216000 seconds"
  ExpiresByType application/x-javascript "access plus 216000 seconds"
  ExpiresByType text/html "access plus 600 seconds"
  ExpiresByType application/xhtml+xml "access plus 600 seconds"
</ifModule>
 
# Cache-Control Headers
<ifModule mod_headers.c>
#month
  <filesMatch "\.(ico|jpe?g|png|gif|swf)$">
    Header set Cache-Control "max-age=2592000, public"
  </filesMatch>
#week
  <filesMatch "\.(css|js)$">
    Header set Cache-Control "max-age=604800, public"
  </filesMatch>
#day
  <filesMatch "\.(x?html?|php)$">
    Header set Cache-Control "max-age=43200, private, must-revalidate"
  </filesMatch>
</ifModule>
# END Cache-Control Headers
 
# Turn ETags Off
<ifModule mod_headers.c>
  Header unset ETag
</ifModule>
FileETag None
 
# Remove Last-Modified Header
<ifModule mod_headers.c>
  Header unset Last-Modified
</ifModule>
like image 978
Joseph Avatar asked Dec 15 '11 00:12

Joseph


1 Answers

Go into httpd.conf and look for the mod_expires line, it should not be commented out. Look for the mod_headers line and make sure it is not commented out.

Or (not for a critical app) there is an easy and dirty test: remove <ifModule mod_expires.c> and </ifModule> leave stuff in between, same goes for <ifModule mod_headers.c> and if your server fails with 500 internet Server Error then your probably missing one or both of those modules and they are not enabled. If so then go into httpd.conf and enable what you need.

You can also test your site's response headers using a tool like REDbot. Simply pick a resource URL like one pointing to an image and paste it in the tool to see what headers get sent back along with some recommendations. Note that it follows the domain's robots.txt rules and will not check the resource if it is disallowed.

And like Gerben said, using the net tab in firefox, chrome dev tools, or some equivalent web developer tool helps see what headers are being sent and received.

You also don't need to set Cache-Control public. And you don't need to use max age if you're also using ExpiresByType calls.

For more info read this great tutorial: http://www.mnot.net/cache_docs/

And learn by example: checkout how it is done in the html5-boilerplate at https://github.com/h5bp/html5-boilerplate/blob/master/dist/.htaccess

For other popular server config examples like lighthttpd, Node.js, Nginx, etc. see: https://github.com/h5bp/server-configs

like image 85
Anthony Hatzopoulos Avatar answered Oct 05 '22 09:10

Anthony Hatzopoulos