Each time a user accesses your website or changes the page the server is required to resend all files, even if some of those files have not changed. This is normally what causes a server to bog down and become slow.
To prevent your server from having to send the same data to the same user more than once, you need to implement Expires Headers. Headers make up the meta data of a request and response transaction in the HTTP protocol. When your server responds to a request for data, it has the option of sending with the response instructions that tell the users browser to save some resources for future use. One the users next page request, the users browser will not need to request many of the images, javascript files and stylesheets that it already has.
Here is the complete .htaccess file that I have created for you. Below this embedded file I will describe the various sections and what they do. This is a Gist.Github.com hosted file, be sure to star it if you are on Github!
.HTACCESS for Caching Files
mod_cache on line 13
The Apache Cache Module is designed to store cached versions of dynamic pages, static pages, javascript, stylesheets and all other resources on your server. When the content becomes stale, the server will fully refresh this content into the stored memory. The technical types can read more about mod_cache from the Apache website. Additionally, you can find a real world implementation described by Softslate.
mod_mime on line 28
The server is responsible for telling users how to handle the files that it is providing to them. Mod mime ensures that your server provides the correct instructions from the correct files. Such a small thought can eliminate road bumps and slow downs when your server cannot find the instructions that it needs to provide.
Disable E-tags on 88 and 104
The E-tag header is part of the HTTP protocol and it’s designed to help optimize cache utilization. On this page we’re attempting to speed up a server and E-tag supposedly helps us do that, so why are we disabling it?
Caching has a hierarchy of what get’s cached when. We recommend disabling e-Tags because it’s fairly easy to mess up and there are only specific use cases where it’s needed (dynamic content where you hash the response for example). Outside of the specific use cases where they add value there’s really no benefit beyond what you get.
mod_headers on line 87
We have segmented our file types and given each file type a different time to stale. When setting the max-age of the mod_headers commands, the numeric values given are in seconds.
Header set Cache-Control "max-age=seconds"
3600 = one hour
86400 = one day
604800 = one week
2419200 = 28 days / one month
You may choose your own expires times as you see fit.
mod_expires on line 114
And the moment that we’ve all been waiting for, setting the EXPIRES HEADERS in an htaccess file. The mod_expires headers are designed to tell the users machine how long to store the files that it’s given. Here I have specific a month for most files, this is very long caching and will only truly benefit websites that have a lot of repeat visitors.
If you have additional questions, head to the Magento Support Forums.
What are EXPIRES HEADERS and how do I implement them?
Load your Magento 2 Store in under 3 Seconds
ReplyDelete