For Apache, you can use the file htaccess
with the cache control directives that inform browsers about when it should download a new version of each file.
Some examples:
Header unset Pragma
FileETag None
Header unset ETag
# 1 Ano (limitado a ficheiro media)
<FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|swf|mp3|mp4)$">
Header set Cache-Control "public"
Header set Expires "Thu, 15 Apr 2010 20:00:00 GMT"
Header unset Last-Modified
</FilesMatch>
# 2 Horas (limitado a ficheiro conteúdos)
<FilesMatch "\.(html|htm|xml|txt|xsl)$">
Header set Cache-Control "max-age=7200, must-revalidate"
</FilesMatch>
# Em cache para sempre (scripts e folhas de estilo)
<FilesMatch "\.(js|css)$">
Header set Cache-Control "public"
Header set Expires "Thu, 15 Apr 2010 20:00:00 GMT"
Header unset Last-Modified
</FilesMatch>
Example cache in CSS files limited to 3 days
- File
.htaccess
in the root of the folder containing the target files;
- Write the appropriate cache control inside the file;
Specify file extensions assigned to the control to be applied:
# extensões separadas por um |
<FilesMatch "\.(css)$">
Specify the duration of the cache so that the server tells the browser that a file is only valid for the specified time, after which another file must be collected:
# max-age = XX segundos
Header set Cache-Control "max-age=259200, must-revalidate"
The output of the file to use is:
Header unset Pragma
FileETag None
Header unset ETag
# 3 Dias
<FilesMatch "\.(css)$">
Header set Cache-Control "max-age=259200, must-revalidate"
</FilesMatch>
Great! @Zuul
– Alexandre Lopes
I’ll use it this way:
# 5 Dias
# 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>
– Alexandre Lopes