Htaccess rewrite image URLs in SEO friendly way

I have these images uploaded in my site with some random numbers and text as their names and I want them to be loaded in the site with a proper name.

How the image loads now with its absolute URL

http://www.example.com/uploads/folder_n/t_xxxx_20170407030916_888867.jpeg

How I want the image to load is,

http://www.example.com/uploads/folder_n/itemname-thumb-xxxx_20170407030916_888867.jpeg

I tried this but it’s not working.

RewriteRule ^uploads/([a-zA-Z0-9_]+)/([a-zA-Z0-9_/-]+)-thumb-([a-zA-Z0-9_/-/.]+)$ /uploads/$1/t_$3 [R=301,NC]
  • “/folder_n/” -> dynamically changes this folder name
  • “t_” -> should be “thumb-”
  • “itemname” -> dynamically change
  • “xxxx_20170407030916_888867.jpeg” -> this also changes dynamically

How can I do this using htaccess rewrite rules?

Thanks

You can use this rule just below RewriteEngine On line:

RewriteRule ^(uploads/[/w-]+)/[/w-]+-thumb-([/w.-]+)/?$ /$1/t_$2 [L,NC]