Nginx rewrite / return unknown subfolders to specific directory
I am trying to achieve the following redirect. A migrated website had images orginally hosted in subfolders with (to the new site) unknown/random subfolders, like this:
/artikel_images/2938/30_01.jpg
/artikel_images/4050/20_11.jpg
/artikel_images/1291/image.jpg
On the new website all images are uploaded/hosted in:
/wp-content/uploads/2023/01/
Returning in the following result:
/wp-content/uploads/2023/01/30_01.jpg
/wp-content/uploads/2023/01/20_11.jpg
/wp-content/uploads/2023/01/image.jpg
I am trying to create a nginx redirect for all possible origin links (that are still in the content) to the single new link.
Hope to get some help, thanks!
I have been trying with several regular expressions, like below to achieve the redirect.
Edit: with thanks to Richard this is the solution:
location /artikel_images { rewrite ^/artikel_images/[0-9]+/(.+)$ /wp-content/uploads/2023/01/$1 last; }
Read more here: Source link