File Caching to Speed Up Website and Web Page Load Time
File Caching is an important step to Speed Up Website and Web Page Load Time.
There are many factors influence the speed of your website. The faster your website or web page loads the more happier your readers will be.
There are many different and important things required to do to make your website faster. We have already discussed many methods to improve the server response time or to speed up the website. We also discussed about the tools to check your Website / Web page Load Speed.
File caching is one of the most important steps towards an optimized website. It is a major stepping stone to speed up website. If cached, the documents and files may be fetched from the cache rather than from the source until this time has passed. If your files are not changing frequently like images or script files you can set a maximum expiry date so that whe user requests for a web page in your site, the page will load faster since it is already available in the cache.
If your website is hosted on Apache web Server then file caching is simple to achieve.
There is a simple global configuration file name .htaccess file in Apache. To know more >> .htaccess Tutorial -A guide and .htaccess tricks and tips.
How to achieve file caching using .htaccess file
Apache server provides different functionalities through configured modules. There are two different Apache modules available to configure caching. They are mod_expires and mod_headers. Today we will discuss more about module mod_expires.
Caching using Apache Module mod_expires
Apache provides Module mod_expires for the generation of Expires HTTP headers according to user-specified criteria.
You can configure this module directives inside directive. The mod_expires module provides three directives as below
This directive enables or disables the generation of the Expires header for the document realm in question.If set to Off, no Expires header will be generated for any document in the realm.So we need to set ExpiresActive ON.
This directive defines the value of the Expires header generated for documents of the specified type.
For example “ExpiresByType image/gif A2592000” indicates that expire GIF images after a month in the client’s cache.
Or “ExpiresByType text/html M604800”
Here “M’ means that the file’s last modification time should be used as the base time and “A” indicates client’s access time should be used
The time that can be used is mentioned below.
.htaccess Time examples
1 2 3 4 5 6 7
300 5 Minute 3600 1 Hour 86400 1 DAY 259200 3 DAY 604800 1 WEEK 2419200 1 MONTH 29030400 12 MONTH
This directive sets the default algorithm for calculating the expiration time for all documents in the affected realm.For example, any of the following directives can be used to make documents expire 1 month after being accessed, by default:
ExpiresDefault “access plus 1 month”
The example given below gives a complete picture on how to achieve Caching using Apache Module mod_expires
1 2 3 4 5 6 7 8 9
We will discuss about achieving caching using the mod_headers module in the next post.