其实设置静态内容缓存时间就是让网页里的图片还有js、css这些资源、包括页面缓存到本地而不是每次都去服务器请求资源,这样做可以减轻服务器的带宽压力和使网页打开速度加快,这样以来用户体验就会好了一些,对seo是有好处的。 那么如何来设置缓存时间呢?下面我就以apache为例吧,因为我们的官网用的是php+apache的,所以小韩就拿apache来说了 本文关键词:gzip, cache-expire 用.htaccess设置网页/图片/js/css文件内容缓存 开启gzip的方法: 1、 http.conf下 将#去掉 LoadModule deflate_module modules/mod_deflate.so 添加如下设置: <IfModule mod_deflate.c> <Location /> #Insert filter SetOutputFilter DEFLATE # Netscape 4.x has some problems… BrowserMatch ^Mozilla/4 gzip-only-text/html # Netscape 4.06-4.08 have some more problems BrowserMatch ^Mozilla/4\.0[678] no-gzip # MSIE masquerades as Netscape, but it is fine # BrowserMatch \bMSIE !no-gzip !gzip-only-text/html # NOTE: Due to a bug in mod_setenvif up to Apache 2.0.59 # the above regex won’t work. You can use the following # workaround to get the desired effect: # BrowserMatch \bMSIE !no-gzip !gzip-only-text/html force-gzip BrowserMatch \bMSIE !no-gzip !gzip-only-text/html # Don’t compress images and other SetEnvIfNoCase Request_URI .(?:gif|jpe?g|png)$ no-gzip dont-vary SetEnvIfNoCase Request_URI .(?:exe|t?gz|zip|bz2|sit|rar)$ no-gzip dont-vary SetEnvIfNoCase Request_URI .(?:pdf|doc)$ no-gzip dont-vary AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css AddOutputFilterByType DEFLATE application/x-javascript # Make sure proxies don’t deliver the wrong content Header append Vary User-Agent env=!dont-vary #DeflateFilterNote ratio ratio_info #LogFormat ‘”%v %h %l %u %t “%r” %>s %b “%{Referer}i” “%{User-Agent}i”” (%{ratio}n)’ deflate #CustomLog logs/deflate_log deflate </Location> 保存退出。重新启动apache就生效了 2 开启cache-expire 第1步 LoadModule expires_module modules/mod_expires.so LoadModule expires_module modules/mod_headers.so 在httpd.conf中,找到上面的代码,然后将前面的#去掉 第2步 在httpd.conf中加入以下代码,可以加到任何空白地方,不了解apache的朋友,如果担心加错地方,就放到http.conf文件的最后一行 <IfModule mod_expires.c> ExpiresActive On ExpiresByType image/gif A2592000 ExpiresByType image/jpeg A2592000 ExpiresByType image/png A2592000 ExpiresByType image/x-icon A2592000 ExpiresByType application/x-javascript A604800 ExpiresByType text/css A604800 </IfModule> 如果apache无法启动了,检查下是不是权限受了影响,因为如果访问权限设置的不对的话会报错,具体可以查看apche的日志,然后百度去找问题根源了。 在开发过程中会有各种想不到的问题出现,这时我建议大家多百度和谷歌一下。 |
|
来自: 昵称14371432 > 《seo》