I am parsing my website with some tools like for example validator.w3 among others to get a much faster execution of the website without errors or execution delays.
The Google PageSpeed Insights tool tells me to specify browser cache by displaying the following warning message.
Specify browser cache Setting an expiration date or maximum age in the HTTP headers of static resources instructs the browser to load previously downloaded resources from local disk rather than over the network.
It details a number of things to correct, based on the theme, it tells me in each image / css / js that I must specify the cache in each of them.
Searching Stack Overflow among other articles indicate an example like this:
<IfModule mod_expires.c>
ExpiresActive On
ExpiresDefault "access plus 10 days"
ExpiresByType text/css "access plus 1 week"
ExpiresByType text/plain "access plus 1 week"
ExpiresByType image/gif "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 month"
ExpiresByType image/x-icon "access plus 3 months"
ExpiresByType application/x-javascript "access plus 1 month"
ExpiresByType application/javascript "access plus 1 week"
ExpiresByType text/javascript "access plus 1 week"
ExpiresByType application/x-icon "access plus 3 months"
</IfModule>
Now if the cache spec is based on that example it would have a too long .htaccess I have too many images on the website.
I found another example in English SO much more optimized and very simple, but I don't know if it is valid.
<IfModule mod_expires.c>
ExpiresActive On
ExpiresDefault "access plus 1 year"
<IfModule mod_headers.c>
<FilesMatch "\.(bmp|css|flv|gif|ico|jpg|jpeg|js|pdf|png|svg|swf|tif|tiff)$">
Header set Last-Modified "Mon, 31 Aug 2009 00:00:00 GMT"
</FilesMatch>
</IfModule>
I have a file directory js/css/img
structured like this:assets/css/style.css
Meanwhile my .htaccess
# Activamos mod_rewrite
RewriteEngine on
# Seleccionamos el directorio base para el RewriteRule
RewriteBase /project/
# Aquí nos evitamos comprobar que sea un archivo (agrego comprobación
# para detectar también directorio) en cada conjunto de reglas
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(.*)$ $1 [QSA,L]
# Obtenemos todo lo que vaya tras "assets/" y subdirectorios previstos
RewriteCond %{REQUEST_URI} assets/(css|fonts|js|img)/(.+)$
# Entonces (si se cumplen todas las condiciones) redirigimos (R)
# y dejamos de evaluar el resto de reglas (L)
RewriteRule ^(.*)$ assets/%1/%2 [L,R]
# Tu/s regla/s
RewriteRule ^online-video-en-hd-gratis/?$ video.php [L,NC,QSA]
RewriteRule ^online/video/hd/free/?$ online.php [L,NC,QSA]
Now my question is? How to specify browser cache in a more optimized way.
There is nothing wrong with adding 10 lines to the .htaccess .
These types of rules do not significantly impact server performance, there should be no difference in access time beyond some fraction of a millisecond, and it is normal and recommended practice.
The example you found with fewer lines only applies to files with an extension (an image is not necessarily served from a given extension, a
archivo.php
might generate an image), and gives an exact expiration date, which is not what you're looking for.What is the cache? When you access a page for the first time, your browser downloads all the resources (html, images, css, js, etc.) and stores them in a special local folder. Then, if you try to access that page again, instead of having to download everything again, the browser will try to use the local copies, decreasing the page load time, thus avoiding having to download content that is already available.
So how is it controlled? when to use local copy and when to see if the server has a new version of the file? While it has a lot of details that are covered in the HTTP Caching article , the important thing to know is that one can configure how long you want a resource (a file) to already be in the client's (browser's) cache, not to return it. to ask the server. This has in favor that it will load faster, and against that if any of these files are modified, there will be users seeing the old version until the cache expires. That is why it is important to specify a reasonable time, according to how your website is, how often you would expect each file to be updated, and whether or not it is important that you use the latest version.
How it is configured. With each file, the server sends headers . Within these headers, specify how it should behave:
Cache-Control
with a time limit
* 259200 seconds = 30 days
Prefer to always verify that it has not been modified
* Check the modification date before using the cache
Or directly disallow the client to cache:
More information at Cache-Control (MDN) .
To send this header, it can be set in the global configuration of the server, in a .htaccess, or sent directly from some function of the programming language used (for example, in PHP with header() ).
It gives you many errors because it generates a line for each file, but they are all easily resolved.
Response
It depends on what cache times you want to set for each of your resources. I have listed some examples so that you can choose which one seems more appropriate in your case (or that you can generate something by mixing these options).
That everything on the web expires in 1 week
In the .htaccess located at the root of your website
That everything expires in 1 day, but that the css expire in 1 week and the images in 1 month
That your website does not allow caching, but the assets folder has a cache of 1 month
In
/project/.htaccess
:In
/project/assets/.htaccess
You could also do it like the example above, but with different .htaccess files for each subfolder:
/project/assets/img/.htaccess
/project/assets/css/.htaccess
/project/assets/js/.htaccess
Ultimately, it depends on what you want to cache, but don't be afraid to add 10, 20 or 30 lines of this style to the .htaccess.