Why use Expires header?

Expires header tell the browser whether a resource needs to be requested from the source (server), or if it can be fetched from the browser’s cache. When we set an Expires header for a resource, such as all images, JavaScript files, or CSS, or whatever file we want, the browser will store those resources in its cache. The next time the visitor comes back to the page it will load faster, as the browser will already have those contents available in its cache, and will not need to download them from the server.

Benefits

  1. No need to send request to server for file.
  2. Saves work for server. Server does not need to process requests, simply because they are not sent from the browser.
  3. Everything will run faster because many files are just taken from the local user’s browser cache.

Example settings for Apache through .htaccess

<code lang="apache"><ifModule mod_expires.c>
    ExpiresActive On
    ExpiresDefault "access plus 1 month"
    ExpiresByType image/gif "access plus 1 month"
    ExpiresByType image/png "access plus 1 month"
    ExpiresByType image/jpeg "access plus 1 month"
    ExpiresByType image/x-icon "access plus 1 month"
    ExpiresByType application/pdf "access plus 1 year"
    ExpiresByType application/x-javascript "access plus 1 month"
    ExpiresByType application/javascript "access plus 1 month"
    ExpiresByType application/ecmascript "access plus 1 month"
    ExpiresByType text/plain "access plus 1 month"
    ExpiresByType text/css "access plus 1 month"
    ExpiresByType text/javascript "access plus 1 month"
    ExpiresByType text/html A300
    <FilesMatch "\.(pl|php|cgi|spl|scgi|fcgi)$">
        ExpiresActive Off
    </FilesMatch>
</ifModule></code>

Comments

You can leave a response, or trackback from your own site.

Before you add comment see for rules.

Leave a Reply

Your email address will not be published. Required fields are marked *

0d7d5l