53
Add Expires headers
There are 21 static components without a far-future expiration date.

    http://static.doers.lk/examples-offline.css
    http://static.doers.lk/kendo.common.min.css
    http://static.doers.lk/kendo.default.min.css
    http://static.doers.lk/style.css
    http://static.doers.lk/jquery.min.js
    http://static.doers.lk/kendo.web.min.js
    http://static.doers.lk/console.js
    http://static.doers.lk/sprite.png
    https://fbexternal-a.akamaihd.net/safe_image.php?...
    https://fbexternal-a.akamaihd.net/safe_image.php?...
    https://fbexternal-a.akamaihd.net/safe_image.php?...
    https://fbexternal-a.akamaihd.net/safe_image.php?...
    https://fbexternal-a.akamaihd.net/safe_image.php?...
    https://fbexternal-a.akamaihd.net/safe_image.php?...
    https://fbexternal-a.akamaihd.net/safe_image.php?...
    https://fbexternal-a.akamaihd.net/safe_image.php?...
    https://fbexternal-a.akamaihd.net/safe_image.php?...
    https://fbexternal-a.akamaihd.net/safe_image.php?...
    https://fbexternal-a.akamaihd.net/safe_image.php?...
    https://fbexternal-a.akamaihd.net/safe_image.php?...
    http://static.doers.lk/favicon.PNG

When testing the site using yahoo YSLOW it says above message. So I don't know how to add expire headers. Any help would be appreciated?

Nirali Biniwale
  • 627
  • 5
  • 16
sami
  • 1,324
  • 3
  • 13
  • 19
  • hope ther would be documenattion in their site – rOcKiNg RhO Dec 20 '12 at 12:53
  • 1
    Google `php Add Expires headers` – Pekka Dec 20 '12 at 12:53
  • I'm assuming that `static.doers.lk` is your own website? – Ja͢ck Dec 20 '12 at 12:56
  • yes i used that cookeeless sub domain to set static content – sami Dec 20 '12 at 12:57
  • 2
    [Here's an explanation for the YSlow message](http://gtmetrix.com/add-expires-headers.html). – DOK Oct 15 '13 at 22:08
  • I am using windows hosting, So i am not able to add .htaccess file. There are any other option to set expiration time in HTML meta tag? – Hardik Mandankaa Feb 25 '17 at 10:40
  • https://developer.yahoo.com/performance/rules.html?guccounter=1&guce_referrer=aHR0cDovL3lzbG93Lm9yZy8&guce_referrer_sig=AQAAAE9k1CHyQCp5gbb_ebWnhDf3zjTquUxYrAUnzng2QGU7cKTwcK3M_J0OhSGTImNREuADoXvHMKEvpuz6Q4pWpBTenH3PYO4D2wgoxQlBuoGajt4GZg74bnio8U2abiFb1aGLOZKjW_YgLufo3Jt8MhFChXScC9si-tOqsGPE942C#expires – jjoselon Jan 09 '20 at 16:02

5 Answers5

74

The easiest way to add these headers is a .htaccess file that adds some configuration to your server. If the assets are hosted on a server that you don't control, there's nothing you can do about it.

Note that some hosting providers will not let you use .htaccess files, so check their terms if it doesn't seem to work.

The HTML5Boilerplate project has an excellent .htaccess file that covers the necessary settings. See the relevant part of the file at their Github repository

These are the important bits

# ----------------------------------------------------------------------
# Expires headers (for better cache control)
# ----------------------------------------------------------------------

# These are pretty far-future expires headers.
# They assume you control versioning with filename-based cache busting
# Additionally, consider that outdated proxies may miscache
# www.stevesouders.com/blog/2008/08/23/revving-filenames-dont-use-querystring/

# If you don't use filenames to version, lower the CSS and JS to something like
# "access plus 1 week".

<IfModule mod_expires.c>
  ExpiresActive on

# Your document html
  ExpiresByType text/html "access plus 0 seconds"

# Media: images, video, audio
  ExpiresByType audio/ogg "access plus 1 month"
  ExpiresByType image/gif "access plus 1 month"
  ExpiresByType image/jpeg "access plus 1 month"
  ExpiresByType image/png "access plus 1 month"
  ExpiresByType video/mp4 "access plus 1 month"
  ExpiresByType video/ogg "access plus 1 month"
  ExpiresByType video/webm "access plus 1 month"

# CSS and JavaScript
  ExpiresByType application/javascript "access plus 1 year"
  ExpiresByType text/css "access plus 1 year"
</IfModule>

They have documented what that file does, the most important bit is that you need to rename your CSS and Javascript files whenever they change, because your visitor's browsers will not check them again for a year, once they are cached.

pixelistik
  • 7,541
  • 3
  • 32
  • 42
  • No, just the "Expires headers" section. Or even just the part that I pasted into my answer. Please make sure you understand fully what it does, especially why [cache busting](https://github.com/h5bp/html5-boilerplate/blob/v4.0.2/doc/htaccess.md#cache-busting) is needed once you use this. – pixelistik Dec 20 '12 at 13:05
  • i have an another question .can you give me an answer for it ? – sami Dec 20 '12 at 13:15
  • You'll note, however, that the HTML5-boilerplate `.htaccess` script suggests that it's preferable to put this info in `httpd.conf` because of the performance hit on Apache from `.htaccess` files. – Nick Oct 05 '13 at 09:19
  • However Google PageSpeed Insights still claims I have only 10 minutes expiration times on my ressources. I'm using the H5BP .htaccess along with GitHub Pages. – kleinfreund Nov 10 '14 at 19:58
  • 1
    Your server needs to support .htaccess files. Github pages don't: `GitHub Pages does not support customer server configuration files such as .htaccess` https://help.github.com/articles/redirects-on-github-pages/ – pixelistik Nov 11 '14 at 22:27
  • I am using windows hosting, So i am not able to add .htaccess file. There are any other option to set expiration time in HTML meta tag? – Hardik Mandankaa Feb 25 '17 at 10:40
12

try this solution and it is working fine for me

## EXPIRES CACHING ##
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access 1 year"
ExpiresByType image/jpeg "access 1 year"
ExpiresByType image/gif "access 1 year"
ExpiresByType image/png "access 1 year"
ExpiresByType text/css "access 1 month"
ExpiresByType text/html "access 1 month"
ExpiresByType application/pdf "access 1 month"
ExpiresByType text/x-javascript "access 1 month"
ExpiresByType text/css "access plus 1 year"
ExpiresByType application/x-shockwave-flash "access 1 month"
ExpiresByType image/x-icon "access 1 year"
ExpiresDefault "access 1 month"
</IfModule>

<IfModule mod_headers.c>
  <FilesMatch "\.(js|css|xml|gz)$">
    Header append Vary: Accept-Encoding
  </FilesMatch>
</IfModule>

<ifModule mod_gzip.c>
mod_gzip_on Yes
mod_gzip_dechunk Yes
mod_gzip_item_include file .(html?|txt|css|js|php|pl)$
mod_gzip_item_include handler ^cgi-script$
mod_gzip_item_include mime ^text/.*
mod_gzip_item_include mime ^application/x-javascript.*
mod_gzip_item_exclude mime ^image/.*
mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
</ifModule>

<IfModule mod_deflate.c>
SetOutputFilter DEFLATE
AddOutputFilterByType DEFLATE text/html text/css text/plain text/xml text/x-js text/js 
</IfModule>

## EXPIRES CACHING ##
3

You can add them in your htaccess file or vhost configuration.

See here : http://httpd.apache.org/docs/2.2/mod/mod_expires.html

But unless you own those domains .. they are our of your control.

Sorin Trimbitas
  • 1,467
  • 18
  • 35
0
<IfModule mod_expires.c>
    # Enable expirations
    ExpiresActive On 

    # Default directive
    ExpiresDefault "access plus 1 month"

    # My favicon
    ExpiresByType image/x-icon "access plus 1 year"

    # Images
    ExpiresByType image/gif "access plus 1 month"
    ExpiresByType image/png "access plus 1 month"
    ExpiresByType image/jpg "access plus 1 month"
    ExpiresByType image/jpeg "access plus 1 month"

    # CSS
    ExpiresByType text/css "access plus 1 month"

    # Javascript
    ExpiresByType application/javascript "access plus 1 year"
</IfModule>
Paul Roub
  • 36,322
  • 27
  • 84
  • 93
0

In ASP.NET there is similar object, you can use Caching Portions in WebFormsUserControls in order to cache objects of a page for a period of time and save server resources. This is also known as fragment caching.
If you include this code to top of your user control, a version of the control stored in the output cache for 150 seconds. You can create your own control that would contain expire header for a specific resource you want.

<%@ OutputCache Duration="150" VaryByParam="None" %>

This article explain it completely: Caching Portions of an ASP.NET Page

Shadman
  • 131
  • 1
  • 10