.htaccess测试查询参数而不是FilesMatch

时间:2012-10-05 10:41:00

标签: apache .htaccess

我有以下htaccess文件:

<filesMatch "\.png$">
<ifModule mod_headers.c>
Header set Cache-Control "public"
Header set Content-Description "File Transfer"
Header set Content-Disposition "attachment"
Header set Content-Type "image/png"
Header set Content-Transfer-Encoding "binary"
</ifModule>
</filesMatch>

我想测试%{QUERY_STRING}而不是执行文件匹配。这可能吗?

1 个答案:

答案 0 :(得分:0)

您仅使用htaccess文件无法执行此操作。您需要编写脚本或其他东西来设置标题,然后使用以下内容将请求映射到脚本:

RewriteEngine On
RewriteCond %{QUERY_STRING} ^dl=true$
RewriteRule ^(.*)\.png$ /image_dl.php?file=$1.png [L]

然后在你的image_dl.php脚本中:

<?php
header('Cache-Control: public');
header('Content-Description: File Transfer');
header('Content-Disposition: attachment');
header('Content-Type: image/png');
header('Content-Transfer-Encoding: binary');

$file = $_GET['file'];
header("Content-length: ".filesize($file));  

ob_clean();
flush();
readfile($file); 
?>