Skip to content

Magento 2: Nginx Rewrite Rule for Updated Media Base URL

I recently updated my media base URL to {{unsecure_base_url}}media/version1234567/. While attempting to create a rewrite rule in Nginx, I’ve encountered some difficulties.

For a single URL, I used the following configuration which works as expected:

location = /media/version1234567/wysiwyg/banners/WebsiteBanners2/HomePageBanners/Snom_Web_May_HP.png {
    return 301 http://voip.local/media/wysiwyg/banners/WebsiteBanners2/HomePageBanners/Snom_Web_May_HP.png;
}

However, for handling multiple URLs, I tried the following rule but it doesn’t seem to be working correctly:

location /media/ {
    rewrite ^/media/versiond+/(.*)$ /media/$1 break;
}

Despite my efforts, the rewrite rule for multiple URLs isn’t functioning as intended. Could you please provide guidance or assistance on how to implement a rewrite rule that effectively handles all URLs with the version number?

Thank you for your help.