.htaccess enable Http Cache-control to optimize site performance

Http Cache-control header can make the browser cache some resources, we can use it to speed up our site.

What is Http Cache-control.

To use Cache-control, here are some questions you should answered.

1.What types of files you want to be cached?

Some static files should be cached by browser. Such as images, pdf, javascript files etc. You can check static resources here.

What are static files in site?

2.How long these files will be cached?

You should sure how long these files will be cached by the browser, one day, one week or one month? This value can be determined by the frequency of these files be modified.  Images are modified less, so the cache time should be long.

3.How to use .htaccess to enable Cache-control

If you want to cache static files:images, js, css, html,ico. Images will be cached one year, and css,js,html files will be cached one month, you can add code below in your .htaccess file.

‚Äč# One year for image files
<filesMatch ".(jpg|jpeg|png|gif|ico)$">
Header set Cache-Control "max-age=31536000, public"
</filesMatch>
# One month for css and js
<filesMatch ".(css|html|js)$">
Header set Cache-Control "max-age=2628000, public"
</filesMatch>

, ,