0

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


Example 1

Request Path:

https://www.example.com//media/catalog/product/cache/1/thumbnail/543x403/db978388cfd007780066eaab38556cef/n/u/number1.png

Response Path:

https://www.example.com/pub/media/catalog/product/cache/543x403/n/u/number1.png


Example 2

Request Path :

https://www.example.com/media/catalog/product/cache/1/thumbnail/543x403/db978388cfd007780066eaab38556cef/l/e/legal_slide01.jpg

Response Path :

https://www.example.com/pub/media/catalog/product/cache/543x403/l/e/legal_slide01.jpg


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

Thank you

0

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 Feb 20 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 Feb 20 at 11:06
  • Sure, I'll take care of that for next time. – Knight017 Feb 21 at 4:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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