如何将PHP查询url重写为实际图像?使用.htaccess Mod重写规则将PHP查询URL重写为图像
我想HTTP://localhost/img.php图像= winter.jpg被改写为HTTP://localhost/img/winter.jpg图像名称是动态变化的。
我试过这样但它不起作用。
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^.+img\.php\?.*?image\=(.+)[$&] ./img/$1 [NC]
谢谢。 ?
[更新]
访问HTTP后://localhost/img.php图像= winter.jpg以下文本附加在该日志文件。浏览器显示消息“未找到对象!在此服务器上找不到请求的URL。如果您手动输入了URL,请检查拼写并重试。”
127.0.0.1 - - [<time>] [localhost/sid#975148][rid#4d09cb8/initial] (3) [perdir Z:/xampp/htdocs/] strip per-dir prefix: Z:/xampp/htdocs/img.php -> img.php
127.0.0.1 - - [<time>] [localhost/sid#975148][rid#4d09cb8/initial] (3) [perdir Z:/xampp/htdocs/] applying pattern '^.+img\.php\?.*?image=(.+)[$&]' to uri 'img.php'
127.0.0.1 - - [<time>] [localhost/sid#975148][rid#4d09cb8/initial] (3) [perdir Z:/xampp/htdocs/] pass through Z:/xampp/htdocs/img.php
[更新]
错误日志说,
[<time>] [error] [client 127.0.0.1] script 'Z:/xampp/htdocs/img.php' not found or unable to stat
所以我创建了一个名为img.php
一个虚拟文件,并表示没有显示出任何更多的错误。我假定一个虚拟文件是没有必要的。现在,只有当我访问HTTP显示虚拟PHP文件的内容://localhost/img.php图像= winter.jpg
我试过[NC,L]
,[R,NC]
,[R,NC,L]
但他们没有工作。
这样算下来重写日志后,现在你需要寻找到错误日志(即通常已经默认启用)。在该错误日志中,404错误将被解释,例如,该文件一直在寻找。同样从重写日志看来,您应该将该规则标记为'[L]'。 – hakre
@hakre感谢您的跟进。我更新了这个问题。 – Teno
你想读这个:http://www.simonecarletti.com/blog/2009/01/apache-rewriterule-and-query-string/ - URL的查询字符串部分需要特殊处理,你不能正则表达它在'RewriteRule'中。基本上,404意味着你的URL没有被重写(你的正则表达式不匹配)。 – hakre