How to solve Leverage browser caching in wordpress without using htaccess

I have a website made with WordPress and Windows Plesk panel hosting. Most of these type of hosting do not support htaccess due to IIS.

I want to solve Leverage browser caching in wordpress without using htaccess file.

Read More

I have tried below non htaccess caching thing but the site is displaying blank screen.

<?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;
?>

Related posts

1 comment

  1. Hi You can do below things to achieve A Grade in Gtmetrix by doing the below things.

    some hosting provider do not like htaccess and therefore I preferred go for Hyper Cache plugins, and do all necessary configurations.

    Then Add below things in header.php of your WordPress themes.

    <?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;
    ?>
    

    After check and do let me know.

    Thanks!

Comments are closed.