greg
greg

Reputation: 6913

redirecting all images inside a directory (and sub dirs) to a php script using htaccess

I have written a basic htaccess rewrite rule to point all images inside a directory to a php script to load them from another location in the filesystem. This works fine if the directory exists but if I remove the directory it breaks. I'm assuming I need to add some sort of wildcard to the rule so everything inside the images directory uses this rule, regardless of whether the directory exists or not.

RewriteCond %{REQUEST_URI} images
RewriteCond %{REQUEST_FILENAME} .*jpg$|.*gif$|.*png$ [NC]
RewriteRule (.*) /image_handler.php?image=$1

Help appreciated, Greg

Upvotes: 1

Views: 231

Answers (1)

Ansari
Ansari

Reputation: 8218

You want to use REQUEST_URI instead of REQUEST_FILENAME:

RewriteCond %{REQUEST_URI} images
RewriteCond %{REQUEST_URI} ^/.*(jpg|gif|png)$ [NC]
RewriteRule ^(.*)$ image_handler.php?image=$1

REQUEST_FILENAME will be the actual system path of whatever it determines is the object of the request, and if the file and directory don't exist then it's not going to match anything.

Upvotes: 1

Related Questions