【问题标题】:Safari not recognizing htaccess img rewriteSafari 无法识别 htaccess img rewrite
【发布时间】:2021-02-27 11:36:38
【问题描述】:

我的 htaccess 中有几行来完成以下任务:

  1. 将 php 页面/脚本重写为 .png
  2. 强制所有图像的用户端缓存刷新

我的 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!

【问题讨论】:

    标签: php .htaccess server


    【解决方案1】:

    确定了修复:内容长度标头是 PHP 中的问题。删除后,脚本按预期执行。谢谢!

    【讨论】:

      猜你喜欢
      • 2017-07-02
      • 2013-05-13
      • 2018-09-13
      • 2015-12-20
      • 1970-01-01
      • 2011-10-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多