如何使用 htaccess 为 Yii 缓存文件设置过期时间 headers

How to set expiry headers using htaccess for Yii cached files

我在 Yii 框架中有一个网站,Yii 版本是 1.1.16

我在 htaccess 中应用了以下代码

## EXPIRES CACHING ##
<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 1 month"
</IfModule>
## EXPIRES CACHING ##

但是当我使用 Google 页面洞察检查我的网站时,它说

https://www.example.com/assets/593f25af/images/dropdown.png(未指定到期时间)

同时为了管理缓存,我在配置文件夹下的 main.php 文件中添加了这段代码:

'cache' => array(
       'class' => 'CApcCache',
 ),

你们能告诉我吗,我做错了什么所以到期 headers 对这些缓存文件不起作用。

请帮帮我!

能否尝试将以下配置替换为 .htaccess

#
# configure mod_expires
#
# URL: http://httpd.apache.org/docs/2.2/mod/mod_expires.html
#
<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresDefault "access plus 1 seconds"
    ExpiresByType image/x-icon "access plus 2692000 seconds"
    ExpiresByType image/jpeg "access plus 2692000 seconds"
    ExpiresByType image/vnd.microsoft.icon "access plus 2692000 seconds"
    ExpiresByType image/png "access plus 2692000 seconds"
    ExpiresByType image/gif "access plus 2692000 seconds"
    ExpiresByType application/x-shockwave-flash "access plus 2692000 seconds"
    ExpiresByType text/css "access plus 2692000 seconds"
    ExpiresByType text/javascript "access plus 2692000 seconds"
    ExpiresByType application/x-javascript "access plus 2692000 seconds"
    ExpiresByType text/html "access plus 600 seconds"
    ExpiresByType application/xhtml+xml "access plus 600 seconds"
</IfModule>


#
# configure mod_headers
#
# URL: http://httpd.apache.org/docs/2.2/mod/mod_headers.html
#
<IfModule mod_headers.c>
    <FilesMatch "\.(ico|jpe?g|png|gif|swf|css|js)$">
        Header set Cache-Control "max-age=2692000, public"
    </FilesMatch>
    <FilesMatch "\.(x?html?|php)$">
        Header set Cache-Control "max-age=600, private, must-revalidate"
    </FilesMatch>
    Header unset ETag
    Header unset Last-Modified
</IfModule>

感谢大家的回答,但后来我意识到这是服务器级别的问题。我的 Plesk 托管存在一些问题,我联系了托管人员,他们修复了它,现在相同的 .htaccess 代码正在运行。