Lucas
Lucas

Reputation: 101

dynamic jpg urls with .htaccess

I am looking to dynamically serve images to web pages with htaccess based on the filename in the url. Normally, I use a nested directory structure throughout my website to serve a dynamic url based on the pseudo directory names. In this case, I would just like to query the file name (sans extension) and serve it to a PHP script so that:

/images/foo.jpg

becomes

/images/index.php?image=foo

while also let existing jpg images in this directory be served. I am sure it is easy, as I have been able to accomplish some pretty cool things with htaccess, but just not this one yet. Thanks.

Upvotes: 6

Views: 2082

Answers (1)

Book Of Zeus
Book Of Zeus

Reputation: 49885

like this:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^images/(.*)\.(jpg|png|jpeg|gif)$ /images/index.php?image=$1 [NC,L]

note: if you need the extension you use $2

Upvotes: 10

Related Questions