php - RewriteCond : if image exist then show it, 否则生成它

标签 php regex .htaccess mod-rewrite

我有一个小的 php 脚本,可以生成论坛签名的图像 (GD)。要调用它们,有一个类似以下的网址:http://www.example.net/img/imgtype_NN.png

地点: - imgtype 是我想要的签名类型 - NN是图像的ID

当我调用此网址时,我想测试该图像是否已存在于目录中(即是否已生成),在本例中我想显示它。

否则我希望我的脚本生成它然后显示它。

这是我实际的 .htaccess :

RewriteCond %{REQUEST_URI} img/(.*)_(.*).png$
RewriteCond %{DOCUMENT_ROOT}/signatures/img/%1_%2.png -f
RewriteRule img/(.*)_(.*).png$ /signatures/img/$1_$2.png [L]

RewriteRule img/(.*)_(.*).png /signatures/signatures.php?signature=$1&id_pop=$2 [L]

图像存储在 example.com/signatures/img/中,php 脚本位于 example.com/signatures/signatures.php

经过几个小时的尝试,我没有完全得到我需要的东西,我现在放弃了,我需要一些帮助。

谢谢大家!

最佳答案

好吧,您的代码不是最好的,所以您的问题可能是由于某些语法错误造成的。
无论如何,你可以尝试这个版本(将此代码放入你的root htaccess中)

RewriteEngine On

RewriteCond %{DOCUMENT_ROOT}/signatures/img/$1_$2\.png -f
RewriteRule ^img/(.+)_(.+)\.png$ /signatures/img/$1_$2.png [L]

RewriteRule ^img/(.+)_(.+)\.png$ /signatures/signatures.php?signature=$1&id_pop=$2 [L]

确保您已在 Apache 配置中启用 mod_rewrite 并允许 htaccess

关于php - RewriteCond : if image exist then show it, 否则生成它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30332039/

相关文章:

regex - Htaccess 重写显示 404 未找到

php - 如何抓取包含无效 HTML 的网站

正则表达式匹配而不考虑间距

java - 根据键获取值

apache - .htaccess 中的 ForceType text/html;charset=utf-8 导致所有外部链接的 CSS 停止呈现

mysql - authz_dbd,多用户身份验证和通过 .htaccess 的单个文件夹

php - json_decode 将大数字字符串解码为整数

php - jQuery/AJAX : How to determine when a host is offline

PHP/MySQL : Can 12 CPU cores make app faster than 6 CPU cores although CPU usage always below 40%?

Java/Android 正则表达式允许可能包含撇号和/或连字符的名称