Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.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.

like image 490
user973254 Avatar asked Mar 14 '12 14:03

user973254


1 Answers

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.

like image 121
Cfreak Avatar answered Sep 29 '22 11:09

Cfreak