htaccess rewrite. Serve .php file when .gif is accessed

2.1k Views Asked by At

I have a dynamic image, which I wish to serve looking like a static image.

e.g. Current url of the image is

http://site.co.uk/media/dynamicImage.php

I wish for the image to be accessed using

http://site.co.uk/media/dynamicImage.gif

How do I use .htaccess to serve the dynamicImage.php file when dynamicImage.gif is accessed?

Please note that I don't want to rewrite ALL files in a folder as per this example: Apache - rewrite images to php file with .htaccess, I just want to rewrite the files I specify.

1

There are 1 best solutions below

0
On BEST ANSWER

Try adding these rules to the htaccess file in your document root:

RewriteEngine On
RewriteRule ^/?media/image.gif$ /media/image.php [L]

You can create as many of those RewriteRules as you need, for each specific image. This makes it so when someone requests http://site.co.uk/media/image.gif, they are actually being served the contents at /media/image.php.

If you need to keep this all within the media directory, e.g. you already have some rules in the media directory's htaccess file, you can modify the rules to look like:

RewriteRule ^image.gif$ image.php [L]