php - 如何在 .htaccess 中为 CDN 添加利用浏览器缓存?

标签 php .htaccess magento caching cdn

我的 .htaccess 中有以下代码

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 application/pdf "access 1 month"
ExpiresByType application/x-javascript "access 1 month"
ExpiresByType application/x-shockwave-flash "access 1 month"
ExpiresByType image/x-icon "access 1 year"
ExpiresDefault "access 2 days"

cdn.domain.com/abc.jpg (expiration not specified)
cdn.domain.com/abc.png (expiration not specified)

JS、CSS 和其他也存在同样的问题。

我需要在.htaccess中放入什么,以便在这方面大部分达到A级。

最佳答案

如果您想使用 CDN 的浏览器缓存,最好通过添加一些缓存 header (例如 Cache-Control、Expires 和 Last-Modified)来缓存文件。

使用 Mod_Headers 来利用浏览器缓存

如果您位于共享服务器上并且您的主机不会启用 Mod_Expires,您仍然可以通过使用可用的 Mod_headers 来利用浏览器缓存。

# Leverage browser caching using mod_headers #
<IfModule mod_headers.c>
    <FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf)$">
        Header set Expires "Wed, 15 Apr 2020 20:00:00 GMT"
        Header set Cache-Control "public"
    </FilesMatch>
</IfModule>
# End of Leverage browser caching using mod_headers #

下面的测试示例:

# 1 YEAR
<FilesMatch "\.(flv|ico|pdf|avi|mov|ppt|doc|mp3|wmv|wav)$">
Header set Cache-Control "max-age=31536000, public"
</FilesMatch>

# 1 WEEK
<FilesMatch "\.(jpg|jpeg|png|gif|swf)$">
Header set Cache-Control "max-age=604800, public"
</FilesMatch>

# 3 HOUR
<FilesMatch "\.(txt|xml|js|css)$">
Header set Cache-Control "max-age=10800"
</FilesMatch>

# NEVER CACHE - notice the extra directives
<FilesMatch "\.(html|htm|php|cgi|pl)$">
Header set Cache-Control "max-age=0, private, no-store, no-cache, must-revalidate"
</FilesMatch>

测试 header

您可以通过运行“curl”命令来验证 Cache-Control: max-age header 是否在文件中到位:

curl -I http://foo.bar.netdna-cdn.com/file.ext

HTTP/1.1 200 OK
Date: Fri, 16 Sep 2014 14:12:20 GMT
Content-Type: text/css
Connection: keep-alive
Cache-Control: max-age=604800, public  ← 1 Week caching time 
Expires: Thu, 21 May 2015 20:00:00 GMT
Vary: Accept-Encoding
Last-Modified: Thu, 24 Jan 2013 20:00:00 GMT
GMT; path=/; domain=.domain.com
Server: NetDNA-cache/2.2
X-Cache: HIT

您使用了以下代码:

使用 Mod_Expires 进行浏览器缓存
利用浏览器缓存的最常见方法是使用 mod_expires。可以将以下代码添加到您的 .htaccess 中,并将自动为所有用户启用浏览器缓存。

# Leverage browser caching using mod_expires #
<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresByType image/jpg "access plus 1 year"
    ExpiresByType image/jpeg "access plus 1 year"
    ExpiresByType image/gif "access plus 1 year"
    ExpiresByType image/png "access plus 1 year"
    ExpiresByType text/css "access plus 1 month"
    ExpiresByType application/pdf "access plus 1 month"
    ExpiresByType text/x-javascript "access plus 1 month"
    ExpiresByType application/x-shockwave-flash "access plus 1 month"
    ExpiresByType image/x-icon "access plus 1 year"
    ExpiresDefault "access plus 2 days"
</IfModule>
# End of Leverage browser caching using mod_expires #

关于php - 如何在 .htaccess 中为 CDN 添加利用浏览器缓存?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41034465/

相关文章:

functional-programming - 为什么 array_map 在闭包引发异常时抛出警告?

apache - 不要在子域上应用重写引擎......?

magento-1.4 - 为什么从 Magento 1.3 升级后订单中的付款方式信息不可用

php - Laravel Bootstrap 在控制组中并排对齐两个元素

php - 如何从数组中取出所有数字1并将其保存到数据库中?

apache - 如何使用 .htaccess 文件将所有流量从一个域重定向到另一个域?

.htaccess - Apache2 "configtest failed"尝试重新启动时

css - 如何在 Magento 的图像产品上重叠 2 张图像?

php - Magento产品页面上的ERR_TOO_MANY_REDIRECTS

php - gettext() 中的多个复数形式