we need to set rewrite rule for some images in Magento 2.2.5 using .htaccess rules or nginx conf

Example 1

Request Path:


Response Path:


Example 2

Request Path :


Response Path :


Here the l/e/ directories are set according to the first two characters of image name here: legal_slide01.jpg

Thank you

1 Answer 1


You can use the following RewriteRule in the .htaccess file inside the media folder. Just add this two lines right below RewriteEngine on:

RewriteCond %{REQUEST_URI} ^/media/catalog/product/cache/1/thumbnail/(.*)/db978388cfd007780066eaab38556cef/(.*)$
RewriteRule "^.*/thumbnail/(.*)/db978388cfd007780066eaab38556cef/(.*)$" /pub/media/catalog/product/cache/$1/$2 [R=301,NC,L]

This rule will do a 301 redirect, if you just want to serve the imagase with the original URL and do an internal redirect, just remove R=301 from the rule's options.

I hope that helps

  • Thanks for your answer, I have found a more generalized solution for this RedirectMatch 301 /(media/catalog/product/cache)/(.*)/(\d+x\d+)/.*/(.*)/(.*)/(.*) /pub/$1/$3/$4/$5/$6
    – Knight017
    Commented Feb 20, 2019 at 11:04
  • Great that you have found your solution. If you are looking for more general solution it would be good to specify in future what exactly you are looking for :-)
    – HelgeB
    Commented Feb 20, 2019 at 11:06
  • Sure, I'll take care of that for next time.
    – Knight017
    Commented Feb 21, 2019 at 4:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.