Answer the question
In order to leave comments, you need to log in
How to elegantly write many redirects by ID using map in NGINX?
Something I completely got confused with map with these map and if.
There is an array with more than 1000 IDs
100
121
34948
34
465
7741
You need to make a redirect like this
rewrite ^/cat/article/ ID $ https://site.com/cat/article/post ID / permanent;
There are IDs that need to be redirected, let's say 5,4,55,21... is needed, but 1,2,3,4,6... is no longer there. The array contains exactly those IDs that need to be redirected.
I read the documentation, looked at the examples. While thinking about this option
map $uri $new_uri {
…
/cat/article/100$ https://site.com/cat/article/post100/
/cat/article/121$ https://site.com/cat/article/post121/
/cat/article/34948$ https://site.com/cat/article/post34948/
…
}
server {
…
if ($new_uri) {
rewrite ^ $new_uri permanent;
. . .
}
Answer the question
In order to leave comments, you need to log in
I would do like this:
map $uri $should_redirect {
/cat/article/4 1;
/cat/article/5 1;
/cat/article/21 1;
/cat/article/55 1;
...
}
location /cat/article/ {
if ($should_redirect) {
rewrite ^/cat/article/([0-9]+)$ https://site.com/cat/article/post$1/ permanent;
}
...
}
location /cat/article/ {
rewrite ^/cat/article/([0-9]+)$ https://site.com/cat/article/post$1/;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question