crap
brief introduction
How to set
-
Statement format 1: ExpiresByType A/M+time (in seconds)
For example, ExpiresByType image/png A604800 sets the png format cache time to 7 days
-
Statement format 2: ExpiresByType descriptive language
For example, ExpiresByType image/png "access plus 7 days" sets the png format cache time to 7 days
-
Statement format 3: match format
<FilesMatch "\.(jpg|jpeg|png|gif|swf)$"> ExpiresDefault A604800 </FilesMatch>
Indicates that the file cache matching jpg | jpeg | png | gif | swf format is 7 days
<IfModule mod_expires.c> #Open ExpiresActive On #The following are different cache time settings for different file types ExpiresByType image/x-icon A2592000 ExpiresByType application/x-javascript A2592000 ExpiresByType text/css A2592000 ExpiresByType image/gif A604800 ExpiresByType image/png A604800 ExpiresByType image/jpeg A604800 ExpiresByType text/plain A604800 ExpiresByType application/x-shockwave-flash A604800 ExpiresByType video/x-flv A604800 ExpiresByType application/pdf A604800 ExpiresByType application/javascript "access plus 1 year" ExpiresByType image/svg A604800 </IfModule>
Some rules
-
Static files: implement the principle of "never expire" by setting a sufficiently long expiration time. -
Dynamic file: set an appropriate Cache Control header. What is appropriate? The principle is simple. Set the change frequency of the dynamic content. Set a shorter expiration time for the content that is frequently modified, and a longer expiration time for the content that is not changed. -
Never cache html files, or a series of problems will occur! This point is also explicitly mentioned in the Level browser caching rule of PageSpeed:
In general, HTML is not static, and shouldn’t be considered cacheable.
Usually these file types need to be cached: -
Images: jpg, gif, png -
favicon/ico -
JavaScript: js -
CSS: css -
Flash: swf -
PDF: pdf -
Media files: video, audio files
-
-
Time stamp or version number for static file name (Very important! Otherwise, how can visitors know if you want to clear the cache to get the latest code files?)
//Original call processing <link href="/css/layout.css" rel="stylesheet" /> //Call new file processing <link href="/css/layout-v20140913.css" rel="stylesheet" /> //Or do it this way <link href="/css/layout.css?version=v20140913" rel="stylesheet" />
Reference article