How to add expires headers using htaccess
In this tutorial we are going to see one of the main site optimization technique used in most of the websites. Adding expires headers to your website will result in increased performance. This is also reduce server load time and increases page speed.
So, What is expires headers?
Expires headers tells browsers to cache a page or not, it is load a page from a cache or request it from server. Expires headers will not only reduce the number of page downloads from the server, in the mean time it reduces number of request that is sent to server to load a page.
So if expires headers are set for a web page, on first page load the page or a specific files(JS, CSS, Images) will be cached on the browser. On the second request to the same page, the page or the specific files will be loaded from browser depends on the expiration time, thus by reducing the request to the server. This will automatically impact your page load time.
Expires headers set the expiration time to the browser how far to store a file in the cache. As said above, Expires headers will not work for the first time visitors, but it will give high performance for the returning visitors by increasing the page load time.
Why add expires headers?
By adding expires headers to your website, browsers will know what to cache, and how long to store the page/file in cache. So when the page load again in the browser, the browser will see when was the last time the page/file is loaded in the browser. If it is loaded in the recent times, and if it is within the expires headers expiration time then it will load the page/file from a cache. If not it will make a new request and download the newer version to load the page/file.
Also adding expires headers is more important for a website, because it reduces HTTP request to server. When number of HTTP request to server is low, the time taken for a browser to communicate with server is also less.
How to add Expires Headers?
Expires Headers can be set to a specific file or to a extension(file types). So it is up-to you to decide which files are changing frequently and which files are not to set expires headers. Most commonly expires headers are set to images, css and js files with long expiration time. We can add expires headers using htaccess.
To do this first take a backup of your .htaccess file and open it for editing. First of all we need to set a default expiration time for the files which is not specified by us. So to do this add the below lines of code to your .htaccess file.
# Enable expirations
# Default directive
ExpiresDefault "access plus 2 hours"
The set of code, will set the expiration for all the files to 2 hours by default. We can also use years, months, weeks, days, hours, minutes and seconds with this setting.
Now let’s see how we can add the expires to specific files,
ExpiresDefault "access plus 2 months"
The above code will set the expiration for the files which has jpg, jpeg, png, gif, js and css to 2 months. That’s all we now set expires headers to files successfully. While adding expires headers, we must know that for which files we have to add expires and how far. Because in some cases css files can be changed frequently.
Let me know if you have any doubts or suggestion on this post through comments.🔥2