Getting file name of file directed by .htaccess?

I'm making a dynamic image for a forum that doesn't allow dynamic images.

I tried using .htacess to redirect all *.png files to image.png... which works perfectly, but from here I can't seem to be able to get the filename of the .png that was requested to generate the content.

For example:

  1. user puts in banana.png

  2. htaccess forwards to image.php

I need a way of getting that banana into my php script.

Using $_SERVER['REQUEST_URI'] and $_SERVER["SCRIPT_NAME"] just returns that of the PHP file.

Is there a way of redirecting it to image.php?=bananana for example?


Try this one. You should be able to access the image path at $_GET['image']. Added ability to preserve existing query string if need be, (but the image key would obviously be overridden).

If you are still getting 404, I don't know what to say. I tested this and it works for me.

RewriteRule ^(.*\.png)$ image.php?%{QUERY_STRING}&image=$1 [L]

RewriteEngine on
RewriteRule ^(.+)\.png$ image.php?file=$1 [L,QSA]

If this doesn't work, you likely don't have mod_rewrite enabled.


 ? Getting file name of file directed by .htaccess?
 ? PHP imagecreatefrompng(file) fails when file is a dynamic image file?
 ? Protect a folder by .htaccess RewriteRule to php
 ? Use PHP to Dynamically Add To .htaccess File?
 ? Using htaccess to check file in another directory
 ? Protect files with .htaccess from the outside, while still allowing internal access
 ? how to Protect Php file with the htaccess to protevent external use or a direct use
 ? Re-writing Dynamic URL with .htaccess
 ? Spoof .php as .png in .htaccess?
 ? PHP: Handling Dynamic File Request