Add Expires Headers to WordPress

This article have explained how you can add expiry header to your image, javascript and css files to speed up your site when loading.

Adding expires headers do not affect the site load time for a first time visitor but you will be surprised how much the page load time decreases (faster page load) for subsequent page views/return visits from that visitor.

Expires header specifies a time far enough in the future so that browsers won’t try to re-fetch images, CSS, javascript etc files that haven’t changed (this reduces the number of HTTP requests) and hence the performance improvement on subsequent page views.

To add expires header to the css, image, javascript files add the following to your .htaccess file

1
2
3
4
5
6
7
8
9
10
# Expire images header
ExpiresActive On
ExpiresDefault A0
ExpiresByType image/gif A2592000
ExpiresByType image/png A2592000
ExpiresByType image/jpg A2592000
ExpiresByType image/jpeg A2592000
ExpiresByType image/ico A2592000
ExpiresByType text/css A2592000
ExpiresByType text/javascript A2592000
# Expire images header
ExpiresActive On
ExpiresDefault A0
ExpiresByType image/gif A2592000
ExpiresByType image/png A2592000
ExpiresByType image/jpg A2592000
ExpiresByType image/jpeg A2592000
ExpiresByType image/ico A2592000
ExpiresByType text/css A2592000
ExpiresByType text/javascript A2592000

A2592000 means 1 month in the future (60*60*24*30=2592000)

Keep in mind that when you use expires header the files are cached in the browser until it expires so do not use this on files that changes frequently. If you change/update a file that has a far future expiry (eg. CSS or javascript files) then you should rename that file and use the renamed version so the browser doesn’t fetch the old file.

Bonus:
Removing ETags

“An ETag (entity tag) is an HTTP response header returned by an HTTP/1.1 compliant web server used to determine change in content at a given URL. When a new HTTP response contains the same ETag as an older HTTP response, the contents are considered to be the same without further downloading.” (Wikipedia)

ETags were added to provide a mechanism for validating entities that is more flexible than the last-modified date but If you’re not taking advantage of the flexible validation model that ETags provide, it’s better to just remove the ETag altogether. Removing the ETag reduces the size of the HTTP headers in the response and subsequent requests thus improving site performance.

Add the following to your .htaccess file to remove ETags:

1
FileETag none
FileETag none

Source: http://www.tipsandtricks-hq.com/how-to-add-far-future-expires-headers-to-your-wordpress-site-1533

Related Posts:

  • htaccess Tricks #7 – WordPress Tricks
    Secure WordPress Contact Forms Protect your insecure WordPress contact forms against online unrighteousness by verifying the domain from whence the form is called. Remember to replace the “domain.co...
  • WordPress Breadcrumbs Without Plugin (advanced and easy method)
    Breadcrumbs is an important element of a web site navigation and WordPress doesn't have a default way of creating breadcrumbs. You have to include the Breadcrumbs on your own themes, either by adding ...
  • Create Twitter Button with Counter in WP
    As seen on the title, we will discuss how to create a twitter button to your web. Twitter button is different from the kind of tweetmeme or retweet button because it is the official twitter button....
  • Force www or without www
    Everyone can link to your site using http://domain.com or http://www.domain.com. A search engine will not always treat both the same, as you can see for yourself - try doing a 'site:yourdomain.com' se...
  • Access to VPS via SSH
    How to access the VPS server via SSH? it will arise when we use the VPS for the first time. The way to install a lot of services and scripts involves accessing your VPSserver via SSH (Secure SHell). ...

Tags: , , , , ,

21.Sep.11 Hosting, SEO, tips, Wordpress


You can follow any responses to this entry through the RSS 2.0 feed.
You can leave a response, or trackback from your own site.

Leave a Comment

:)