Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
843 views
in Technique[技术] by (71.8m points)

.htaccess rewrite image file to php script

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]

The question is:

how can I make this rewrite /tmp/some_image.png -> /image.php?file=some_image.png

I've tried to make my own rule, but without success.

Thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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]

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...