0

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/version\d+/(.*)$ /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.

2 Answers 2

0

The rule for this kind of redirects already exists on the example repository. If you check the "Configure Nginx" inside https://experienceleague.adobe.com/en/docs/commerce-operations/installation-guide/prerequisites/web-server/nginx you will see that they reference the nginx.conf.sample.

In that file you have the following configuration inside the static folder which can be mapped into the media folder: https://github.com/magento/magento2/blob/2.4-develop/nginx.conf.sample#L106

Should be something similar to

location /media/ {

    location ~ ^/version\d*/ {
        rewrite ^/version\d*/(.*)$ /$1 last;
    }
    ....
}

Regards

0

After further adjustments, I found a solution that works:

location /media/ {
    location ~ ^/media/version\d*/ {
        rewrite ^/media/version\d*/(.*)$ /media/$1 last;
    }
}

This configuration successfully handles the rewrite for multiple URLs by nesting the rewrite rule within the /media/ location block.

Best regards,

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.