Apache - 使用 .htaccess 将图像重写为 php 文件 [英] Apache - rewrite images to php file with .htaccess
问题描述
我正在寻找一种方法将我所有的图像请求从一个文件夹重写到 some.php 文件中,同时保留原始图像 url(或部分路径).
I'm looking for a way to rewrite all my image requests from one folder into some.php file, while preserving the original image url (or partial path).
所以,
example.com/folder/img/test.jpg
将被重写为类似
example.com/folder/some.php?img=img/test.jpg
(这是最好的方法吗?)
(is this the best approach?)
我对正则表达式还不够熟悉,所以我会非常感激:)
I'm not familiarized enought witrh regular expressions, so I'll be very thankfull :)
注意:我之前尝试过一些解决方案,但都没有奏效.另外,我在 CentOS 环境下运行 Apache 2.0.
note : I've tried some solutions before, none of them worked. ALso, I'm running Apache 2.0 under CentOS environment.
推荐答案
通过 httpd.conf
启用 mod_rewrite 和 .htaccess 然后把这段代码放到你的 .htaccess
下DOCUMENT_ROOT
目录:
Enable mod_rewrite and .htaccess through httpd.conf
and then put this code in your .htaccess
under DOCUMENT_ROOT
directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^(folder)/(img/[^.]+\.jpg)$ $1/some.php?img=$2 [L,QSA,NC]
确保:
- .htaccess 已启用
- mod_rewrite 已启用
- 您的网址是
http://example.com/folder/img/test.jpg
这篇关于Apache - 使用 .htaccess 将图像重写为 php 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!