Dynamic jpg urls with .htaccess

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

  /images/foo.jpg

becomes

  /images/index.php?image=foo

and also let existing jpg images in this directory be served. I am sure this is easy, as I was able to accomplish some pretty cool things with htaccess, but not this one. Thanks.

+6
source share
1 answer

like this:

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

Note: if you need an extension, you use $2

+10
source

Source: https://habr.com/ru/post/900826/


All Articles