With .htaccess
(which is recommended)
First of all, create a file named .htaccess
and send it to the root of your site (public_html).
Control the Cache
Inside the file insert the following code:
#5 dias de cache
# O cabeçalho "pragma" é para compatibilidade com o IE
<FilesMatch "(css|js)$">
Header set Cache-Control "max-age=432000, public, must-revalidate"
Header set Pragma "max-age=432000, public, must-revalidate"
</FilesMatch>
In this way when a person accesses your site, the cache will be stored for 5 days, after 5 days, if the person accesses again, the cache will be updated, that is, a new version of your cache will be downloaded.
In the example above, 5 days of cache was given. See below for other examples.
Before proceeding know, um dia tem 86.400 segundos
.
That is, if you want the cache to last only 2 days, multiply 2x 86400 seconds which is equal to: 172800 seconds.
Then in the above code replace: "max-age=432000
with "max-age=172800
and your cache will last 2 days.
Disabling the Cache
To disable the cache, just replace it in the first code quoted "max-age=432000
with "max-age=0
this way you will have your cache disabled.
The final example will be:
#Sem cache
# O cabeçalho "pragma" é para compatibilidade com o IE
<FilesMatch "(css|js)$">
Header set Cache-Control "max-age=0, public, must-revalidate"
Header set Pragma "max-age=0, public, must-revalidate"
</FilesMatch>
Note: I have set up in the above code only for files .css
and .js
(which is what is usually kept as a cache)
If you want to add more extensions, such as images , etc ...
Just add in the above code the desired extension, more precisely in:
<FilesMatch "(css|js)$">
To add images to the cache settings just leave it like this:
<FilesMatch "(css|js|png|jpeg|ico|gif)$">
You should separate the extensions with |
.