Home >php教程 >php手册 >Using .htaccess for browser image file caching

Using .htaccess for browser image file caching

WBOY
WBOYOriginal
2016-07-06 13:28:191305browse

For picture websites, pictures 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

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn