Set Expiration or Expiring Time by mod_expires.c on Apache via .htaccess to Reduce Web Page Loading Time

by Yang Yang on April 16, 2009

When your site’s ready in design and majority of common media resources won’t change for quite some time, say, half a year, for example, the images and flashes. It makes sense to set the expiration of them much longer than the default to prevent the client browsers downloading them every time a visitor drops by, thus conveniently decreasing the page loading time and increasing user experience.

So how do we do that? On Apache web server, examine, customize and put the following section of .htaccess directives in the document root of your site:

<IfModule mod_expires.c>
ExpiresActive On
ExpiresDefault "access plus 3 days"
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType text/css "access plus 1 month"
ExpiresByType text/javascript "access plus 1 week"
ExpiresByType application/x-javascript "access plus 1 week"
</IfModule>

In case you don’t know what a .htaccess file is, just create a text file named .htaccess (yes, just a dot with the elongated extension), put the above code in, save it and upload the file to the root directory of your domain.

Of course, that said, you will need mod_expires module explicitly enabled as it’s not a core feature but an extension.

Comments on this entry are closed.

Previous post:

Next post: