要配置PHP静态资源缓存,您可以使用以下方法之一:
<IfModule mod_expires.c>
ExpiresActive on
ExpiresByType text/css "access plus 1 month"
ExpiresByType text/javascript "access plus 1 month"
ExpiresByType application/javascript "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType image/gif "access plus 1 month"
</IfModule>
上述代码将设置CSS、JavaScript和图像文件的缓存时间为1个月。您可以根据需要自定义这些值。
<?php
$expire_time = 60 * 60 * 24 * 30; // 30天
header("Cache-Control: public, max-age=$expire_time");
header("Expires: " . gmdate("D, d M Y H:i:s", time() + $expire_time) . " GMT");
上述代码将设置缓存时间为30天。您可以根据需要自定义这个值。
无论您选择使用.htaccess文件还是PHP代码进行缓存配置,都需要确保您的服务器上启用了相应的模块(如mod_expires模块)。
请注意,静态资源缓存是在客户端上进行的,因此如果您的资源URL没有更改,客户端将继续使用缓存的版本。如果您更新了资源文件,您可以通过更改资源URL(如在文件名中添加版本号或时间戳)来强制客户端重新获取最新版本的资源。
下一篇:php strpos
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站