.htaccess overwrite image file in PHP script

Here is what I have in my .htaccess , and this should work in the 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 arises:

how can i do this rewriting /tmp/some_image.png -> /image.php?file=some_image.png

I tried to make my own rule, but to no avail.

Thanks.

+7
source share
2 answers

Is / tmp a directory available on your web server? I hope this is a separate / tmp folder and not the actual / tmp server, as this will be a security risk.

In any case, if the image is a physical file, you need to place it after overwriting in order to force HTTPS and check before the conditions whether it is a file or a directory:

 RewriteRule ^/tmp/([^\.]+)\.png$ /image.php?file=$1.png [NC,L] 

You can also check other extensions:

 RewriteRule ^/tmp/([^\.]+)\.(png|jpg|gif)$ /image.php?file=$1.$2 [NC,L] 

Or if you don't care (all this image is in your tmp folder. Although I would not recommend this)

 RewriteRule ^/tmp/(.*)$ /image.php?file=$1 [NC,L] 

If this is not a physical file, you can put any of them at the end of your rules.

+13
source

Correct answer:

 RewriteRule ^/?tmp/(.*\.png)$ captcha.php?file=$1 [L] 

thanks goes to Humbedooh@freenode

-one
source

All Articles