Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 567
Lucas Avatar asked Nov 04 '11 22:11

Lucas


1 Answers

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

like image 90
Book Of Zeus Avatar answered Oct 11 '22 00:10

Book Of Zeus