duanhu7615 2014-11-15 19:01
浏览 109
已采纳

根据文件的存在,通过.htaccess重定向图像请求

I implemented an image generator which saves the final file in .jpg format inside /images. Once the file is saved, there is no need to generate it ever again.

This is the route for the image generator:

/gen.php?id=495abc

This is the directory where all the .jpg images are saved:

/images/495abc.jpg

So, the objective is: If a user tries to request http://website.com/images/495abc.jpg, the .htaccess rules have to verify if the file exists and then execute one of two actions depending on the case:

  1. if the .jpg exists, serve the file directly.

  2. if the .jpg does not exist, redirect the request to the generator at /gen.php?id=495abc (the new image will be displayed at the end of this process with Header("Content-Type: image/jpeg"))

Is that feasible via .htaccess? If not, there is another alternative: Redirect all /images/*.jpg requests to /gen.php?id=* and let gen.php do all the dirty work. Of course, the first alternative is better.

Any ideas? Thanks!

  • 写回答

1条回答 默认 最新

  • doucai1901 2014-11-15 19:05
    关注

    Yes. It is possible with htaccess:

    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^images/(\d+)\.(jpe?g|png|gif|ico|bmp)$ /gen.php?id=$1 [L,NC]
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 在grasshopper里DrawViewportWires更改预览后,禁用电池仍然显示
  • ¥15 NAO机器人的录音程序保存问题
  • ¥15 C#读写EXCEL文件,不同编译
  • ¥15 MapReduce结果输出到HBase,一直连接不上MySQL
  • ¥15 扩散模型sd.webui使用时报错“Nonetype”
  • ¥15 stm32流水灯+呼吸灯+外部中断按键
  • ¥15 将二维数组,按照假设的规定,如0/1/0 == "4",把对应列位置写成一个字符并打印输出该字符
  • ¥15 NX MCD仿真与博途通讯不了啥情况
  • ¥15 win11家庭中文版安装docker遇到Hyper-V启用失败解决办法整理
  • ¥15 gradio的web端页面格式不对的问题