Take the 2-minute tour ×
Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

I have recently analysed my website with pagespeed addon on firebug. It suggested me to set expiration on CSS, JS and image files.

I am wondering how do i do this?

share|improve this question

4 Answers 4

up vote 23 down vote accepted

Update your Apache configuration to include the directives below as part of your core configuration:

# 
# associate .js with "text/javascript" type (if not present in mime.conf)
# 
AddType text/javascript .js

# 
# configure mod_expires
# 
# URL: http://httpd.apache.org/docs/2.2/mod/mod_expires.html
# 
<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresDefault "access plus 1 seconds"
    ExpiresByType image/x-icon "access plus 2692000 seconds"
    ExpiresByType image/jpeg "access plus 2692000 seconds"
    ExpiresByType image/png "access plus 2692000 seconds"
    ExpiresByType image/gif "access plus 2692000 seconds"
    ExpiresByType application/x-shockwave-flash "access plus 2692000 seconds"
    ExpiresByType text/css "access plus 2692000 seconds"
    ExpiresByType text/javascript "access plus 2692000 seconds"
    ExpiresByType application/x-javascript "access plus 2692000 seconds"
    ExpiresByType text/html "access plus 600 seconds"
    ExpiresByType application/xhtml+xml "access plus 600 seconds"
</IfModule>

# 
# configure mod_headers
# 
# URL: http://httpd.apache.org/docs/2.2/mod/mod_headers.html
# 
<IfModule mod_headers.c>
    <FilesMatch "\\.(ico|jpe?g|png|gif|swf|css|js)$">
        Header set Cache-Control "max-age=2692000, public"
    </FilesMatch>
    <FilesMatch "\\.(x?html?|php)$">
        Header set Cache-Control "max-age=600, private, must-revalidate"
    </FilesMatch>
    Header unset ETag
    Header unset Last-Modified
</IfModule>
share|improve this answer
    
I get 500 Internal Server error when i place it in my .htaccess file –  KoolKabin Nov 11 '10 at 5:51
    
Your Apache setup may not support all of the options listed above. I'll update this answer shortly to help solve this issue. –  John Conde Nov 11 '10 at 12:50
    
My Drupal contains javascript/css references like : www.example.com/misc/jquery.js?v=1.4.4 or www.example.com/sites/all/modules/nice_menus/css/nice_menus.css?mtu293 , it does not seem to work for such files. Is it updated as of today? –  AgA Sep 28 '13 at 11:59
    
@JohnConde Why both expires and headers? Also, when updating an image or css for example, does the file need to be renamed? Or for a css file the versioning works: test.css?123 ? –  guisasso Jan 14 at 14:01

It depend from the host and the way you server these things. Option 1) if you control the server make the apache to add expiration headers in the response Option 2) if you do not control the web server, or you server the images/js/css/etc you can set these headers from the script that server them

Have in mind that these hints are recommendable but not absolute truth. They are more to save you a bandwidth than to speed up your website . So if you have a low traffic to your site do not worry too much about this.

share|improve this answer
    
I am with option 2. How do i set these headers from the script. Script means php or which one? –  KoolKabin Nov 11 '10 at 5:52
    
You can check stackoverflow.com/questions/2185449/… this is a python example but if you are familiar with programming I think you will get the idea. In php you will have to use headers(). But have in mind that in this case all "static" files will have to be served trough the script which will increase your CPU quota. Now it comes to me that there is a 3rd option. Use CDN for these files. –  Ilian Iliev Nov 11 '10 at 7:24

You can put this in your htaccess:

<FilesMatch "(?i)^.*\.(ico|flv|jpg|jpeg|png|gif|js|css)$">
ExpiresActive On
ExpiresDefault A2592000
</FilesMatch>

It will target files with those extensions (ico, flv, jpg and so on) and set the Expires header to be access time (A) plus 30 days (2592000 seconds). You can also add this at the server level if you have access to that.

Once implemented, you can check the file headers using this online tool.

share|improve this answer

Setting up expires in Lightspeed Web Server

Login to Admin Console then >Server->General->Expires Settings->Expires By Type

Add the following:

text/css=A604800, text/javascript=A604800, application/javascript=A604800, application/x-javascript=A604800, application/x-shockwave-flash=A604800, image/gif=A604800, image/jpg=A604800, image/jpeg=A604800, image/png=A604800, image/ico=A604800, image/icon=A604800

604800 is the seconds of the expire, which should be suitable for your needs since its 168 hours which is 7 days. Additionally Light Speed Server does use a htaccess which you need to add the following line:

ExpiresActive On

Alternatively if you do not have admin console access try the following the .htaccess file:

  ExpiresByType image/png A604800
  ExpiresByType image/gif A604800
  ExpiresByType image/jpg A604800
  ExpiresByType image/jpeg A604800
  ExpiresByType text/javascript A604800
  ExpiresByType application/x-javascript A604800
  ExpiresByType text/css A604800
share|improve this answer

protected by John Conde Jul 23 '13 at 21:42

Thank you for your interest in this question. Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.

Would you like to answer one of these unanswered questions instead?

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