Home >Backend Development >PHP Tutorial >Using .htaccess for browser image file caching, _PHP tutorial
For image websites, images must be re-downloaded every time the page is opened, which is not only slow, but also a waste of traffic. At this time, you need to use caching to force the browser to cache image files
Cache files, ask how many times the website has been visited, and reduce traffic consumption. 2 caching codes are now provided
Open the .htaccess file and write the following code
Method 1: Unify cache duration
<FilesMatch ".(flv|gif|jpg|jpeg|png|ico|txt|swf|pdf|swf|js)$"> Header set Cache-Control "max-age=2592000" </FilesMatch>
Method 2: Cache duration according to different file types
<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>
Note:
ExpiresDefault "access 1 month", the default file cache duration, use with caution, otherwise some of your methods will be invalid, such as recording page views, you must refresh the page with F5
Extended reading:
How to create .htaccess file?
Download .htaccess file
Original address: http://cssteach.com/show-20-113.html