如何解决在不使用htaccess的情况下利用wordpress中的浏览器缓存


How to solve Leverage browser caching in wordpress without using htaccess

我有一个用WordPress和Windows Plesk面板托管的网站。由于IIS的原因,大多数此类主机都不支持htaccess。

我想解决在wordpress中利用浏览器缓存而不使用htaccess文件的问题。

我已经尝试过以下非htaccess缓存的东西,但网站显示为空白屏幕。

<?php
if (isset($_GET['img'])) {$filename = $_GET['img'];}else{exit;}
$allowed = array('gif','png','jpg');
$ext = pathinfo($filename, PATHINFO_EXTENSION);
if(!in_array($ext,$allowed)) {exit;}
$stc = 31536000;
$ts = gmdate("D, d M Y H:i:s", time() + $stc) . " GMT";
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' . basename($filename));
header('Content-Transfer-Encoding: binary');
header("Expires: $ts");
header("Cache-Control: max-age=$stc, public");
header("Pragma: cache");
ob_clean();
flush();
readfile($filename);
exit;
?>

Hi你可以通过做以下事情来获得Gtmetrix的A级成绩。

一些主机提供商不喜欢htaccess,因此我更喜欢Hyper Cache插件,并进行所有必要的配置。

然后在Wordpress主题的header.php中添加以下内容。

<?php
$stc = 31536000;
$ts = gmdate("D, d M Y H:i:s", time() + $stc) . " GMT";
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' . basename($filename));
header('Content-Transfer-Encoding: binary');
header("Expires: $ts");
header("Cache-Control: max-age=$stc, public");
header("Pragma: cache");
ob_clean();
flush();
exit;
?>

检查后请告诉我。

谢谢!