使用.htaccess 缓存与Gzip压缩页面

打开.htaccess文件加入以下代码,记得先备份。

#缓存下列类型文件,其中A600表示缓存到期时间为600秒
<IfModule mod_expires.c>
ExpiresActive On
ExpiresDefault A600
ExpiresByType image/x-icon A345600
ExpiresByType application/x-javascript A345600
ExpiresByType text/css A604800
ExpiresByType image/gif A345600
ExpiresByType image/png A2345600
ExpiresByType image/jpeg A345600
ExpiresByType text/plain A86400
ExpiresByType application/x-shockwave-flash A345600
ExpiresByType video/x-flv A345600
ExpiresByType application/pdf A345600
ExpiresByType text/html A345600
</IfModule>
#Gzip压缩下列后缀文件
<ifmodule mod_deflate.c>
AddOutputFilter DEFLATE html xml php js css
</ifmodule>

或者可以使用以下表达

#Expire Header
ExpiresActive on //开启缓存
ExpiresByType text/css "access 1 month" //css文件缓存1个月
ExpiresByType text/plain "access 2 days" //纯文本内容缓存2天
ExpiresByType text/html "access 2 days" //html文件缓存2天
ExpiresByType application/javascript "access 1 month" //JS文件缓存1个月
ExpiresByType image/jpeg "access 1 month" //jpeg图片缓存1个月
ExpiresByType image/x-icon "access 1 month" //icon缓存1个月
ExpiresByType image/gif "access 1 month" //gif图片缓存一个月
ExpiresByType image/png "access 1 month" //png图片缓存一个月
ExpiresByType image/ico "access 1 month" //ico缓存1个月
ExpiresByType application/pdf "access 1 month" //pdf文件缓存一个月
ExpiresByType application/x-shockwave-flash "access 1 month" //flash缓存一个月
ExpiresDefault "access 1 month" //默认(未提及的可缓存文件)缓存1个月

其中 text/css 表示文件类型,access 1 month 表示缓存一个月,可以根据实际需要修改

使用.htaccess 缓存与Gzip压缩页面

使用.htaccess 缓存与Gzip压缩页面



微信扫描下方的二维码阅读本文

赞(0)