Compress your CSS & javascript files Print

Version française disponible ici

Today I’m going to show you how to compress all your CSS and javascript files to gain in speed and preserve your bandwidth without changing your habits.

Just open your .htaccess file and add this few lines:

  1. <IfModule mod_gzip.c>
  2. mod_gzip_on Yes
  3. mod_gzip_dechunk Yes
  4. mod_gzip_item_include file \.(html?|txt|css|js|php|pl)$
  5. mod_gzip_item_include handler ^cgi-script$
  6. mod_gzip_item_include mime ^text/.*
  7. mod_gzip_item_include mime ^application/x-javascript.*
  8. mod_gzip_item_exclude mime ^image/.*
  9. mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
  10. </IfModule>

Here it is understood that CSS and javascript files are located in a folder on your server and not stored in your database. Who would come to use this option yet? If you want to retain access to your CSS from your Textpattern admin interface, use the excellent rvm_css plugin by Ruud van Melik available here.

Your files are now compressed with gzip. Cause this file is now open, here is some general and useful tips to consider.


1° Redirect URL with or without ‘www’

This can preserve your website from duplicate content:

  1. # 301 redirect to domain without 'www.'
  2. RewriteCond %{HTTP_HOST} ^$ [NC]
  3. RewriteRule ^(.*)$$1 [R=301,L]
  1. # 301 redirect to domain with 'www.'
  2. RewriteCond %{HTTP_HOST} ^$ [NC]
  3. RewriteRule ^(.*)$$1 [R=301,L]


2° Automatic caching

  1. # caching files for one week
  2. <FilesMatch ".(js|css|txt)$">
  3. Header set Cache-Control "max-age=604800"
  4. </FilesMatch>
  5. # caching images & flash files for one month
  6. <FilesMatch ".(pdf|flv|gif|jpg|jpeg|png|ico|swf)$">
  7. Header set Cache-Control "max-age=2592000"
  8. </FilesMatch>


3° Protect all media files from external display

  1. # protect all media files
  2. RewriteCond %{HTTP_REFERER} !^$
  3. RewriteCond %{HTTP_REFERER} !^http://(www\.)?*$ [NC]
  4. RewriteRule \.(gif|jpg|jpeg|png|ico|mp3|mpg|avi|mov)$ - [F]

Note : change all mentions of with your own domain name.

Published on | Updated on by Patrick LEFÈVRE.

Related Articles

Display a page on your website for known referrers - Publié le 2012.02.09 By Patrick LEFÈVRE.
Control you blank pages - Publié le 2011.12.22 By Patrick LEFÈVRE.
Well looking quotes - Publié le 2014.03.10 By Patrick LEFÈVRE.
Pat_tile, a plugin for your articles title - Publié le 2013.04.26 By Patrick LEFÈVRE.
Split your links list into columns - Publié le 2011.12.14 By Patrick LEFÈVRE.
Access to all article archives

» Previous Article» Next Article
txp:fr supports Textpattern ;·) Textpattern CMS

Need Help? Post your question on Entr'Aide txp:fr website