Use browser caching, like apache or .htaccess?

What to do here? I have a huge list of files that the Google Speed ​​Page said "Use browser caching" .. but I do not know how to do this? Can I mess up or modify the Apache configuration file (below) or add something to the .htaccess page?

<IfModule mod_proxy.c> ProxyRequests Off CacheRoot "/var/run/proxy" CacheSize 1024 CacheGcInterval 24 #CacheMaxExpire 24 #CacheLastModifiedFactor 0.1 #CacheDefaultExpire 1 #NoCache a_domain.com another_domain.edu joes.garage_sale.com <Directory "disabled_proxy"> Allow from example.com Deny from all Order Deny,Allow </Directory> </IfModule> ## #### mod_expires is configured so that all static files but images #### expire after 60 seconds. Any response that has a life span of more #### than 5 seconds (see webperfcache.conf) will be cached by webperfcache. #### Make sure your CGIs return a "Cache-Control: no-cache" header if you #### elect to make your dynamically generated HTML pages not cache-able. #### If all your HTML pages are static you may also increase ExpiresDefault. <IfModule mod_expires.c> ExpiresActive On ExpiresDefault A60 ExpiresByType image/bmp A3600 ExpiresByType image/gif A3600 ExpiresByType image/ief A3600 ExpiresByType image/jpeg A3600 ExpiresByType image/png A3600 ExpiresByType image/tiff A3600 ExpiresByType image/x-cmu-raster A3600 ExpiresByType image/x-portable-anymap A3600 ExpiresByType image/x-portable-bitmap A3600 ExpiresByType image/x-portable-graymap A3600 ExpiresByType image/x-portable-pixmap A3600 ExpiresByType image/x-rgb A3600 ExpiresByType image/x-xbitmap A3600 ExpiresByType image/x-xpixmap A3600 ExpiresByType image/x-xwindowdump A3600 ExpiresByType audio/basic A3600 ExpiresByType audio/midi A3600 ExpiresByType audio/mpeg A3600 ExpiresByType audio/x-aiff A3600 ExpiresByType audio/x-pn-realaudio A3600 ExpiresByType audio/x-pn-realaudio-plugin A3600 ExpiresByType audio/x-realaudio A3600 ExpiresByType audio/x-wav A3600 ExpiresByType video/mpeg A3600 ExpiresByType video/quicktime A3600 ExpiresByType video/x-msvideo A3600 ExpiresByType video/x-sgi-movie A3600 </IfModule> 
+42
php browser-cache .htaccess
Jul 29 '11 at 20:03
source share
4 answers

I did the same a couple of days ago. Added this to my .htaccess file:

 ExpiresActive On ExpiresByType image/gif A2592000 ExpiresByType image/jpeg A2592000 ExpiresByType image/jpg A2592000 ExpiresByType image/png A2592000 ExpiresByType image/x-icon A2592000 ExpiresByType text/css A86400 ExpiresByType text/javascript A86400 ExpiresByType application/x-shockwave-flash A2592000 # <FilesMatch "\.(gif¦jpe?g¦png¦ico¦css¦js¦swf)$"> Header set Cache-Control "public" </FilesMatch> 

And now that I’ve launched the Google Speed ​​page, the more efficient priority use of broaching caches is no longer.

Hope this helps.

+36
Jul 29 '11 at 20:13
source share

I took the opportunity to provide the full .htaccess code for submitting to Google PageSpeed ​​Insight:

  • Enable compression
  • Use browser caching
 # Enable Compression <IfModule mod_deflate.c> AddOutputFilterByType DEFLATE application/javascript AddOutputFilterByType DEFLATE application/rss+xml AddOutputFilterByType DEFLATE application/vnd.ms-fontobject AddOutputFilterByType DEFLATE application/x-font AddOutputFilterByType DEFLATE application/x-font-opentype AddOutputFilterByType DEFLATE application/x-font-otf AddOutputFilterByType DEFLATE application/x-font-truetype AddOutputFilterByType DEFLATE application/x-font-ttf AddOutputFilterByType DEFLATE application/x-javascript AddOutputFilterByType DEFLATE application/xhtml+xml AddOutputFilterByType DEFLATE application/xml AddOutputFilterByType DEFLATE font/opentype AddOutputFilterByType DEFLATE font/otf AddOutputFilterByType DEFLATE font/ttf AddOutputFilterByType DEFLATE image/svg+xml AddOutputFilterByType DEFLATE image/x-icon AddOutputFilterByType DEFLATE text/css AddOutputFilterByType DEFLATE text/html AddOutputFilterByType DEFLATE text/javascript AddOutputFilterByType DEFLATE text/plain </IfModule> <IfModule mod_gzip.c> mod_gzip_on Yes mod_gzip_dechunk Yes mod_gzip_item_include file .(html?|txt|css|js|php|pl)$ mod_gzip_item_include handler ^cgi-script$ mod_gzip_item_include mime ^text/.* mod_gzip_item_include mime ^application/x-javascript.* mod_gzip_item_exclude mime ^image/.* mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.* </IfModule> # Leverage Browser Caching <IfModule mod_expires.c> ExpiresActive On ExpiresByType image/jpg "access 1 year" ExpiresByType image/jpeg "access 1 year" ExpiresByType image/gif "access 1 year" ExpiresByType image/png "access 1 year" ExpiresByType text/css "access 1 month" ExpiresByType text/html "access 1 month" ExpiresByType application/pdf "access 1 month" ExpiresByType text/x-javascript "access 1 month" ExpiresByType application/x-shockwave-flash "access 1 month" ExpiresByType image/x-icon "access 1 year" ExpiresDefault "access 1 month" </IfModule> <IfModule mod_headers.c> <filesmatch "\.(ico|flv|jpg|jpeg|png|gif|css|swf)$"> Header set Cache-Control "max-age=2678400, public" </filesmatch> <filesmatch "\.(html|htm)$"> Header set Cache-Control "max-age=7200, private, must-revalidate" </filesmatch> <filesmatch "\.(pdf)$"> Header set Cache-Control "max-age=86400, public" </filesmatch> <filesmatch "\.(js)$"> Header set Cache-Control "max-age=2678400, private" </filesmatch> </IfModule> 

There are also several configurations for various web servers, see here .
Hope this helps get a 100/100 score.

optimized page score

+75
Apr 7 '15 at 9:59
source share

This is what I use for header / caching management, I am not an Apache professional, so let me know if there is room for improvement, but I know that it works well on all my sites for some time now.

mod_expires

http://httpd.apache.org/docs/2.2/mod/mod_expires.html

This module controls the setting of the Expires HTTP header and the max-age directive of the HTTP Cache-Control header in server responses. The expiration date can be set in relation to the time during which the source file was changed, or the client’s access time.

These HTTP headers instruct the client about the validity of the document and its storage. If cached, the document can be extracted from the cache, and not from the source, until this time passes. After that, the copy of the cache is considered “expired” and is invalid, and a new copy must be obtained from the source.

 # BEGIN Expires <ifModule mod_expires.c> ExpiresActive On ExpiresDefault "access plus 1 seconds" ExpiresByType text/html "access plus 1 seconds" ExpiresByType image/gif "access plus 2592000 seconds" ExpiresByType image/jpeg "access plus 2592000 seconds" ExpiresByType image/png "access plus 2592000 seconds" ExpiresByType text/css "access plus 604800 seconds" ExpiresByType text/javascript "access plus 216000 seconds" ExpiresByType application/x-javascript "access plus 216000 seconds" </ifModule> # END Expires 

mod_headers

http://httpd.apache.org/docs/2.2/mod/mod_headers.html

This module provides directives for managing and modifying the headers of HTTP requests and responses. Headers can be combined, replaced or deleted.

 # BEGIN Caching <ifModule mod_headers.c> <filesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|swf)$"> Header set Cache-Control "max-age=2592000, public" </filesMatch> <filesMatch "\.(css)$"> Header set Cache-Control "max-age=604800, public" </filesMatch> <filesMatch "\.(js)$"> Header set Cache-Control "max-age=216000, private" </filesMatch> <filesMatch "\.(xml|txt)$"> Header set Cache-Control "max-age=216000, public, must-revalidate" </filesMatch> <filesMatch "\.(html|htm|php)$"> Header set Cache-Control "max-age=1, private, must-revalidate" </filesMatch> </ifModule> # END Caching 
+36
Oct 22 '12 at 16:22
source share

First we need to check if the mod_headers.c and mod_expires.c options are enabled.

 sudo apache2 -l 

If we don’t have it, we need to enable them

 sudo a2enmod headers 

Then we need to restart apache

 sudo apache2 restart 

Finally add rules on .htaccess (see other answers), e.g.

 ExpiresActive On ExpiresByType image/gif A2592000 ExpiresByType image/jpeg A2592000 ExpiresByType image/jpg A2592000 ExpiresByType image/png A2592000 ExpiresByType image/x-icon A2592000 ExpiresByType text/css A86400 ExpiresByType text/javascript A86400 ExpiresByType application/x-shockwave-flash A2592000 # <FilesMatch "\.(gif¦jpe?g¦png¦ico¦css¦js¦swf)$"> Header set Cache-Control "public" </FilesMatch> 
+5
Jul 21 '15 at 18:55
source share



All Articles