Php 如何在apache或.htaccess上利用浏览器缓存?
在这里做什么?我有一个巨大的文件列表,Google Speed Page称之为“利用浏览器缓存”。。但我不知道怎么做?我是否弄乱或更改Apache配置文件(如下所示),或者是否在.htaccess页面中添加了内容Php 如何在apache或.htaccess上利用浏览器缓存?,php,.htaccess,browser-cache,Php,.htaccess,Browser Cache,在这里做什么?我有一个巨大的文件列表,Google Speed Page称之为“利用浏览器缓存”。。但我不知道怎么做?我是否弄乱或更改Apache配置文件(如下所示),或者是否在.htaccess页面中添加了内容 <IfModule mod_proxy.c> ProxyRequests Off CacheRoot "/var/run/proxy" CacheSize 1024 CacheGcInterval 24
<IfModule mod_proxy.c>
ProxyRequests Off
CacheRoot "/var/run/proxy"
CacheSize 1024
CacheGcInterval 24
#CacheMaxExpire 24
#CacheLastModifiedFactor 0.1
#CacheDefaultExpire 1
#NoCache a_domain.com another_domain.edu joes.garage_sale.com
<Directory "disabled_proxy">
Allow from example.com
Deny from all
Order Deny,Allow
</Directory>
</IfModule>
##
#### mod_expires is configured so that all static files but images
#### expire after 60 seconds. Any response that has a life span of more
#### than 5 seconds (see webperfcache.conf) will be cached by webperfcache.
#### Make sure your CGIs return a "Cache-Control: no-cache" header if you
#### elect to make your dynamically generated HTML pages not cache-able.
#### If all your HTML pages are static you may also increase ExpiresDefault.
<IfModule mod_expires.c>
ExpiresActive On
ExpiresDefault A60
ExpiresByType image/bmp A3600
ExpiresByType image/gif A3600
ExpiresByType image/ief A3600
ExpiresByType image/jpeg A3600
ExpiresByType image/png A3600
ExpiresByType image/tiff A3600
ExpiresByType image/x-cmu-raster A3600
ExpiresByType image/x-portable-anymap A3600
ExpiresByType image/x-portable-bitmap A3600
ExpiresByType image/x-portable-graymap A3600
ExpiresByType image/x-portable-pixmap A3600
ExpiresByType image/x-rgb A3600
ExpiresByType image/x-xbitmap A3600
ExpiresByType image/x-xpixmap A3600
ExpiresByType image/x-xwindowdump A3600
ExpiresByType audio/basic A3600
ExpiresByType audio/midi A3600
ExpiresByType audio/mpeg A3600
ExpiresByType audio/x-aiff A3600
ExpiresByType audio/x-pn-realaudio A3600
ExpiresByType audio/x-pn-realaudio-plugin A3600
ExpiresByType audio/x-realaudio A3600
ExpiresByType audio/x-wav A3600
ExpiresByType video/mpeg A3600
ExpiresByType video/quicktime A3600
ExpiresByType video/x-msvideo A3600
ExpiresByType video/x-sgi-movie A3600
</IfModule>
代理请求关闭
CacheRoot“/var/run/proxy”
缓存大小1024
缓存间隔24
#cachemax24
#CacheLastModifiedFactor 0.1
#CacheDefaultExpire 1
#NoCache a_domain.com另一个_domain.edu joes.garage_sale.com
允许来自example.com
全盘否定
命令拒绝,允许
##
####mod_expires配置为所有静态文件(图像除外)
####60秒后过期。任何寿命超过
####webperfcache将缓存超过5秒(请参阅webperfcache.conf)。
####如果需要,请确保CGI返回“缓存控制:无缓存”标题
####选择使动态生成的HTML页面不可缓存。
####如果所有HTML页面都是静态的,那么也可以增加ExpiresDefault。
过期于
到期默认A60
ExpiresByType图像/bmp A3600
ExpiresByType图像/gif A3600
ExpiresByType图像/ief A3600
过期按类型图像/jpeg A3600
ExpiresByType图像/png A3600
ExpiresByType图像/tiff A3600
ExpiresByType图像/x-cmu-raster A3600
ExpiresByType图像/x-portable-anymap A3600
ExpiresByType图像/x-portable-bitmap A3600
ExpiresByType图像/x-portable-graymap A3600
ExpiresByType图像/x-portable-pixmap A3600
ExpiresByType图像/x-rgb A3600
ExpiresByType图像/x-xbitmap A3600
ExpiresByType图像/x-xpixmap A3600
过期按类型映像/x-xwindowdump A3600
过期按类型音频/基本A3600
过期按类型音频/midi A3600
ExpiresByType音频/mpeg A3600
ExpiresByType音频/x-aiff A3600
ExpiresByType audio/x-pn-realaudio A3600
ExpiresByType audio/x-pn-realaudio-plugin A3600
ExpiresByType audio/x-realaudio A3600
过期按类型音频/x-wav A3600
ExpiresByType视频/mpeg A3600
ExpiresByType视频/quicktime A3600
过期按类型视频/x-msvideo A3600
过期按类型视频/x-sgi-MOVICE A3600
几天前我也在做同样的事情。将此添加到我的.htaccess文件:
ExpiresActive On
ExpiresByType image/gif A2592000
ExpiresByType image/jpeg A2592000
ExpiresByType image/jpg A2592000
ExpiresByType image/png A2592000
ExpiresByType image/x-icon A2592000
ExpiresByType text/css A86400
ExpiresByType text/javascript A86400
ExpiresByType application/x-shockwave-flash A2592000
#
<FilesMatch "\.(gif¦jpe?g¦png¦ico¦css¦js¦swf)$">
Header set Cache-Control "public"
</FilesMatch>
ExpiresActive开启
ExpiresByType图像/gif A2592000
过期按类型图像/jpeg A2592000
ExpiresByType图像/jpg A2592000
ExpiresByType图像/png A2592000
ExpiresByType图像/x图标A2592000
ExpiresByType文本/css A86400
ExpiresByType文本/javascript A86400
过期按类型应用/x-shockwave-flash A2592000
#
标头集缓存控制“公共”
现在当我运行GoogleSpeed页面时,利用browwer缓存不再是一个优先事项
希望这能有所帮助。这是我用来控制头/缓存的,我不是Apache专业人士,所以请告诉我是否有改进的余地,但我知道这在我所有的网站上已经运行了一段时间了 Mod_过期 此模块控制服务器响应中Expires HTTP头和缓存控制HTTP头的max age指令的设置。过期日期可以设置为与上次修改源文件的时间或客户端访问的时间相关 这些HTTP头是向客户机发出的关于文档有效性和持久性的指示。如果已缓存,则在该时间过去之前,可以从缓存而不是从源获取文档。之后,缓存副本被视为“过期”和无效,必须从源获取新副本
# BEGIN Expires
<ifModule mod_expires.c>
ExpiresActive On
ExpiresDefault "access plus 1 seconds"
ExpiresByType text/html "access plus 1 seconds"
ExpiresByType image/gif "access plus 2592000 seconds"
ExpiresByType image/jpeg "access plus 2592000 seconds"
ExpiresByType image/png "access plus 2592000 seconds"
ExpiresByType text/css "access plus 604800 seconds"
ExpiresByType text/javascript "access plus 216000 seconds"
ExpiresByType application/x-javascript "access plus 216000 seconds"
</ifModule>
# END Expires
#开始过期
过期于
ExpiresDefault“访问加1秒”
ExpiresByType text/html“访问加1秒”
ExpiresByType image/gif“访问时间加2592000秒”
ExpiresByType图像/jpeg“访问时间加2592000秒”
ExpiresByType图像/png“访问加2592000秒”
ExpiresByType文本/css“访问时间加604800秒”
ExpiresByType文本/javascript“访问时间加216000秒”
ExpiresByType应用程序/x-javascript“访问时间加216000秒”
#期满
Mod_标题
此模块提供控制和修改HTTP请求和响应头的指令。可以合并、替换或删除标题
# BEGIN Caching
<ifModule mod_headers.c>
<filesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|swf)$">
Header set Cache-Control "max-age=2592000, public"
</filesMatch>
<filesMatch "\.(css)$">
Header set Cache-Control "max-age=604800, public"
</filesMatch>
<filesMatch "\.(js)$">
Header set Cache-Control "max-age=216000, private"
</filesMatch>
<filesMatch "\.(xml|txt)$">
Header set Cache-Control "max-age=216000, public, must-revalidate"
</filesMatch>
<filesMatch "\.(html|htm|php)$">
Header set Cache-Control "max-age=1, private, must-revalidate"
</filesMatch>
</ifModule>
# END Caching
#开始缓存
标题集缓存控制“最大年龄=2592000,公共”
标题集缓存控制“最大年龄=604800,公共”
标题集缓存控制“最大年龄=216000,专用”
标题集缓存控制“最大年龄=216000,公共,必须重新验证”
标题集缓存控制“最大年龄=1,私有,必须重新验证”
#端缓存
我抓住机会提供了完整的.htaccess
代码,以便在Google PageSpeed Insight上传递:
#启用压缩
AddOutputFilterByType DEFLATE应用程序/javascript
AddOutputFilterByType DEFLATE应用程序/rss+xml
AddOutputFilterByType DEFLATE应用程序/vnd.ms-fontobject
AddOutputFilterByType放气应用程序/x-font
AddOutputFilterByType放气应用程序/x-font-opentype
AddOutputFilterByType放气应用程序/x-font-otf
AddOutputFilterByType放气应用程序/x-font-truetype
AddOutputFilterByType放气应用程序/x-font-ttf
AddOutputFilterByType DEFLATE应用程序/x-javascript
AddOutputFilterByType DEFLATE应用程序/xhtml+xml
AddOutputFilterByType DEFLATE应用程序/xml
AddOutputFilterByType泄气字体/opentype
AddOutputFilterByType DEFLATE字体/otf
AddOutputFilterByType DEFLATE字体/ttf
AddOutputFilterByType DEFLATE image/svg+xml
AddOutputFilterByType放气图像/x图标
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/javascript
添加输出
# Enable Compression
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/vnd.ms-fontobject
AddOutputFilterByType DEFLATE application/x-font
AddOutputFilterByType DEFLATE application/x-font-opentype
AddOutputFilterByType DEFLATE application/x-font-otf
AddOutputFilterByType DEFLATE application/x-font-truetype
AddOutputFilterByType DEFLATE application/x-font-ttf
AddOutputFilterByType DEFLATE application/x-javascript
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE font/opentype
AddOutputFilterByType DEFLATE font/otf
AddOutputFilterByType DEFLATE font/ttf
AddOutputFilterByType DEFLATE image/svg+xml
AddOutputFilterByType DEFLATE image/x-icon
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/javascript
AddOutputFilterByType DEFLATE text/plain
</IfModule>
<IfModule mod_gzip.c>
mod_gzip_on Yes
mod_gzip_dechunk Yes
mod_gzip_item_include file .(html?|txt|css|js|php|pl)$
mod_gzip_item_include handler ^cgi-script$
mod_gzip_item_include mime ^text/.*
mod_gzip_item_include mime ^application/x-javascript.*
mod_gzip_item_exclude mime ^image/.*
mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
</IfModule>
# Leverage Browser Caching
<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>
<IfModule mod_headers.c>
<filesmatch "\.(ico|flv|jpg|jpeg|png|gif|css|swf)$">
Header set Cache-Control "max-age=2678400, public"
</filesmatch>
<filesmatch "\.(html|htm)$">
Header set Cache-Control "max-age=7200, private, must-revalidate"
</filesmatch>
<filesmatch "\.(pdf)$">
Header set Cache-Control "max-age=86400, public"
</filesmatch>
<filesmatch "\.(js)$">
Header set Cache-Control "max-age=2678400, private"
</filesmatch>
</IfModule>
sudo apache2 -l
sudo a2enmod headers
sudo apache2 restart
ExpiresActive On
ExpiresByType image/gif A2592000
ExpiresByType image/jpeg A2592000
ExpiresByType image/jpg A2592000
ExpiresByType image/png A2592000
ExpiresByType image/x-icon A2592000
ExpiresByType text/css A86400
ExpiresByType text/javascript A86400
ExpiresByType application/x-shockwave-flash A2592000
#
<FilesMatch "\.(gif|jpe?g|png|ico|css|js|swf)$">
Header set Cache-Control "public"
</FilesMatch>