通过.htaccess访问图像时执行脚本

时间:2011-03-09 14:30:28

标签: php .htaccess

是否可以访问服务器上的图像,它可以使用.htaccess单独执行脚本,例如(伪代码):

-> user views http://www.mywebsite.com/images/systems_analysis.jpg;
    -> .htaccess gets the image name (systems_analysis.jpg);
    -> then executes viewed.php?img=systems_analysis.jpg;
-> leaving viewed.php to process with the script.

因此,当查看图像时,它将执行viewed.php脚本,该脚本仅在/images/目录中访问图像时执行。

1 个答案:

答案 0 :(得分:2)

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} .*jpg$|.*gif$|.*png$ [NC]
RewriteRule (.*) /viewed.php?image=$1

上述.htaccess条目将重写以下请求:

yourdomain.com/some_image.jpg - > youdomain.com/viewed.php?image=some_image