如何使用htaccess为Yii缓存文件

时间:2018-03-28 04:57:58

标签: php .htaccess caching yii1.x

我在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 ##

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

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

另外,为了管理缓存我在config文件夹下的main.php文件中添加了这段代码:

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

你能告诉我,我做错了,所以expiry header对这些缓存文件不起作用。

请帮助我!

2 个答案:

答案 0 :(得分:1)

您是否可以尝试将以下内容替换为 .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>

答案 1 :(得分:0)

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