Search code examples
.htaccess

Replace image filename in url using regular expression


I have many images hosted with the similar urls and want to replace image names using .htaccess/regex in URL from:

https://wwww.mydomain.com/img/ID_1234_bla_bla_bla.jpg

to:

https://wwww.mydomain.com/img/id1234.jpg

where (ID_1234) randge is 1-9999

i.e. "ID">"id" (to lowercase) "slash" out number range 1-9999

No idea how to do that


Solution

  • RewriteRule ^(.*\/)ID_(\d\d\d\d)_[^\.\s]*(.*)$ $1id$2$3
    

    See demo