.htaccess 将图像文件重写为 php 脚本 [英] .htaccess rewrite image file to php script
问题描述
这是我现在在 .htaccess
中的内容,将来应该可以使用:
Here is what I have for now in my .htaccess
and this should work in future:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
问题是:
我该如何重写 /tmp/some_image.png ->/image.php?file=some_image.png
我试图制定自己的规则,但没有成功.
I've tried to make my own rule, but without success.
谢谢.
推荐答案
/tmp 是您的网络服务器可访问的目录吗?我希望它是一个单独的/tmp 文件夹,而不是服务器的实际/tmp,因为这会带来安全风险.
Is /tmp a directory accessible by your web-server? I'm hoping it's a separate /tmp folder and not the actual /tmp of the server as that would be a security risk.
无论如何,如果图像是物理文件,那么您需要在重写后放置它以强制使用 HTTPS,并且在检查它是文件还是目录的条件之前:
Anyway if the image is a physical file then you need to put this after your rewrite to force HTTPS and before the conditions checking if it's a file or directory:
RewriteRule ^/tmp/([^\.]+)\.png$ /image.php?file=$1.png [NC,L]
您也可以检查其他扩展程序:
You could check for other extensions as well:
RewriteRule ^/tmp/([^\.]+)\.(png|jpg|gif)$ /image.php?file=$1.$2 [NC,L]
或者如果您不在乎(所有内容都是 tmp 文件夹中的图像.虽然我不推荐这样做)
Or if you don't care (everything is an image in your tmp folder. Though i wouldn't recommend this)
RewriteRule ^/tmp/(.*)$ /image.php?file=$1 [NC,L]
如果它不是物理文件,您可以将其中任何一个放在规则的末尾.
If it's not a physical file you can put any one of these at the end of your rules.
这篇关于.htaccess 将图像文件重写为 php 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!