The cache-control header tells the browser to catch your files for a specific amount of time that you can define in the header. Just like the expires headers, the cache-control header also helps your website to be extremely fast for returning users. You can specify cache-control headers for the static files on your website so that they will be served from the cache instead of making a request to the origin server. So this is how you can set the cache-control header on your WordPress website.
If you are using an Apache server, you can add the following lines to your .htaccess file to set cache-control headers for your static files.
# TN - BEGIN Cache-Control Headers
<ifModule mod_headers.c>
<filesMatch "\.(ico|jpeg|jpg|png|gif|swf|pdf|svg)$">
Header set Cache-Control "public"
</filesMatch>
<filesMatch "\.(css)$">
Header set Cache-Control "public"
</filesMatch>
<filesMatch "\.(js)$">
Header set Cache-Control "private"
</filesMatch>
<filesMatch "\.(x?html?|php)$">
Header set Cache-Control "private, must-revalidate"
</filesMatch>
</ifModule>
# TN - END Cache-Control Headers
If you are using an NGINX server to host your website, use the following code snippet in your NGINX configuration file to enable cache-control for your static assets.
location ~* \.(jpg|jpeg|png|gif|ico|css|js|pdf|svg)$ {
expires 90d;
add_header Cache-Control "public, no-transform";
}
The NGINX users that have multiple websites can create a separate configuration file and put this in that file. Then include the newly created configuration in the NGINX configuration of every website that you want to set cache-control header. You can also add any additional format of files after researching it on the internet. Some files are not meant to be cached by the browsers. So be careful about it.