How to add Expires Headers ?
There are various ways of adding expires headers to website. Most common way of adding expires headers to website is adding them in web server configuration file. Other way is programatically changing expiry date of cache resources. In many CMS like WordPress have plugin solutions for adding expires headers.
Using .htaccess file for adding Expires Headers
First you need to decide which files/resources needs to be have expire headers and for how much time after they need to expired from cache. ( know more how expires headers works here)
If you do not want to choose any particular files/resources you can use ExpiresDefault directive. Which direct selects default files types from server configuration.
Now for adding expires headers in .htaccess file you need to know where to find that file. Mostly .htaccess files found at root directory for any website. Use ftp or cpanel for editing that file. For apache servers mod_expires is used for adding expires headers. So by adding following lines to your website .htaccess file you can add expires headers to files.
##Adding Expires Headers ##
ExpiresActive On
ExpiresDefault "access plus 1 month"
ExpiresByType image/jpg "access plus 3 month"
ExpiresByType image/jpeg "access plus 3 month"
ExpiresByType image/gif "access plus 2 month"
ExpiresByType image/png "access plus 2 month"
ExpiresByType text/css "access plus 1 month"
ExpiresByType application/pdf "access plus 4 month"
ExpiresByType text/x-javascript "access plus 2 month"
ExpiresByType application/x-shockwave-flash "access plus 1 month"
ExpiresByType video/mp4 "access plus 1 year"
##Adding Expires Headers ##
Here ExpiresByType will override ExpiresDefault. you can update this lines based on changing expire time. You can also add different more type resources/files by adding new line of ExpiresByType with file type/encoding pair along with expire time.
Programmatically adding Expires Headers
There is way you can add expires headers programmatically. Many server languages like php have way to add expires headers for pages and content generated using php. Here is example of adding expires headers using php for generating expires headers. The good part of this is we can also control expires time using code. So base on requirement we can change different expires time.
header('Expires: '.gmdate('D, d M Y H:i:s \G\M\T', time() + (60 * 60))); // 1 hour
Using plugins for adding Expires Headers
There are many plugins are available for cms like WordPress for adding expires headers. It is really helpful for non-technical people to add expires headers to their websites. some of plugins are as listed below.
Add comment