paan
paan

Reputation: 7182

How to write a mod_rewrite rule to look for an image in a specific folder?

I need to write a rule to redirect any image file to a specific folder. Namely "images"

RewriteCond $1 ^(.*\.jpg|.*\.gif|.*\.bmp)

That will match all the imag, the the rerwrite part is confusing me. I want that

Http://domain.com/path/controller/view/image.jpg
http://domain.com/any/path/that/i/want/image.jpg

to load the file

http://domain.com/iamges/image.jpg

Is this possible?

Upvotes: 1

Views: 265

Answers (1)

Ken
Ken

Reputation: 78852

RewriteEngine On
RewriteBase /

# prevent endless loops
RewriteCond %{REQUEST_URI} !images/

# capture only the filename 
RewriteRule ^.*/(.*\.jpg|.*\.gif|.*\.bmp) images/$1 [L,R]

The R option in the [L,R] is forcing a visible rewrite - if you want it to appear that the image is coming from the request url then just use [L]

Take a look at the mod_rewrite documentation for more details

Upvotes: 2

Related Questions