【发布时间】:2021-02-27 11:36:38
【问题描述】:
我的 htaccess 中有几行来完成以下任务:
- 将 php 页面/脚本重写为 .png
- 强制所有图像的用户端缓存刷新
我的 htaccess 行:
RewriteEngine On
RewriteBase /directory/to/dynamic_image
RewriteRule ^bg\.png$ bg.php
AddHandler application/x-httpd-php .jpeg
<Files bg.png>
FileETag None
Header unset ETag
Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate"
Header set Pragma "no-cache"
Header set Expires "Wed, 11 Jan 1984 05:00:00 GMT"
</Files>
# DISABLE CACHING
<IfModule mod_headers.c>
Header set Cache-Control "no-cache, no-store, must-revalidate"
Header set Pragma "no-cache"
Header set Expires 0
</IfModule>
我特别注意到在 Safari 上,任何图像重写都会显示无效。知道可能会发生什么吗?图像重写在 Chrome 和其他浏览器上运行良好。
为了后代,我还将包括被重写为 png 的 php 脚本:
$imagesDir = 'img/';
$images = glob($imagesDir . '*.{jpg,jpeg,png,gif}', GLOB_BRACE);
$randomImage = $images[array_rand($images)]; // See comments
$file_out = $randomImage; // The image to return
if (file_exists($file_out)) {
$image_info = getimagesize($file_out);
//Set the content-type header as appropriate
header('Content-Type: ' . $image_info['mime']);
//Set the content-length header
header('Content-Length: ' . filesize($file_out));
//Write the image bytes to the client
readfile($file_out);
}
else { // Image file not found
header($_SERVER["SERVER_PROTOCOL"] . " 404 Not Found");
}?>```
Any help appreciated. Thanks!
【问题讨论】: