Home > Backend Development > PHP Tutorial > htaccess Use htaccess for browser image file caching

htaccess Use htaccess for browser image file caching

WBOY
Release: 2016-07-28 08:28:40
Original
1028 people have browsed it

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. We now provide 2 caching codes

Open the .htaccess file and write the following code

Method 1: Unified cache duration

<FilesMatch ".(flv|gif|jpg|jpeg|png|ico|txt|swf|pdf|swf|js)$">
Header set Cache-Control "max-age=2592000"
</FilesMatch>
Copy after login

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>
Copy after login

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, F5 is required to refresh the page

Extended reading:

How to create a .htaccess file?

Download the .htaccess file

Original address: http://cssteach.com/show-20-113.html

The above introduces the use of htaccess for browser image file caching, including the content of htaccess. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template