Wednesday, February 11, 2015

How to speed up your site with htaccess


Loading speed is a common problem considering that your website is downloading again and  again the same files (css, js, png,...).

Let's load a couple of modules:

  • mod_expires
  • mod_header

The idea is to use the max-age header parameter which lets us says "this file expires 1 week from today", which is simpler than setting an explicit date. The max-age is measured in seconds, ie. 1 day = 86400, 1 week=604800, and so on.

In uuntu we can write:
sudo a2enmod headers
sudo a2enmod expires
sudo service apache2 restart
Then edit your .htaccess and insert the folowing lines:
#
# Configure mod_expires
#
# URL: http://httpd.apache.org/docs/2.2/mod/mod_expires.html
#

    ExpiresActive On
    ExpiresDefault A3600
    ExpiresByType image/x-icon A2592000
    ExpiresByType application/x-javascript A86400
    ExpiresByType application/javascript A86400
    ExpiresByType text/javascript A86400
    ExpiresByType text/css A86400
    ExpiresByType image/gif A604800
    ExpiresByType image/png A604800
    ExpiresByType image/jpeg A604800
    ExpiresByType application/font-woff A604800
    ExpiresByType application/octet-stream A604800

#
# Configure mod_headers
#
# URL: http://httpd.apache.org/docs/2.2/mod/mod_headers.html
#

   
        Header set Cache-Control "max-age=86400, public"
   

   
        Header set Cache-Control "max-age=600, private, must-revalidate"
   

    Header unset ETag
    Header unset Last-Modified

Done!. You can try now and check if your page is using the cache.

Hope help