How to Add Expires Headers in WordPress
What is Expires Headers
Expires headers are a type of HTTP header that indicates how long until cached copies of site resources expire. Expires headers tell the browsers what resource can be stored and fetched from the browser’s cache or the source itself. Using the expires headers, you maximize the speed of your site by reducing the HTTP requests between your device and the Service and also help you load the site more easily.
Caching
Browser caching enables the browser to cache to locally store resources improving site speed. This tells the web browser how long it should store your website resources before they are deleted.
How to enable Expires header in wordpress using .htaccess
Find your .htaccess file. Download a backup copy of the .htaccess file to your local computer. Add the following code snippet in the file
ExpiresActive On ExpiresByType image/jpg "access 1 year" ExpiresByType image/jpeg "access 1 year" ExpiresByType image/gif "access 1 year" ExpiresByType image/png "access 1 year" ExpiresByType image/svg "access 1 year" ExpiresByType text/css "access 1 month" ExpiresByType application/pdf "access 1 month" ExpiresByType application/javascript "access 1 month" ExpiresByType application/x-javascript "access 1 month" ExpiresByType application/x-shockwave-flash "access 1 month" ExpiresByType image/x-icon "access 1 year" ExpiresDefault "access 2 days"
How to enable Expires header in Nginx
Nginx works in a different way to Apache in that it does not make use of a specific file like Apache does with the .htaccess file. Instead, you need to edit the server configuration file then copy and paste the following line of code to your server block.
location ~* \.(jpeg|jpg|png|svg|gif)$ { expires 365d; } location ~* \.(html|css|js)$ { expires 30d; }
You can adjust the expiration times for different types of files as needed.