How to use rewrite with maps
Nginx has a quite powerful rewrite engine. When paired with map, it is very helpful, for example in moving to a new url scheme (as in my case).
In example below I want to have following effect:
for urls with /pref/ prefix I want to replace oldlink with newlink and old2link with new2link and leave rest of urls unchanged.
Example
http {
map $oldseo $newseo {
default "--NOOP--";
oldlink newlink;
old2link new2link;
}
server {
listen 80;
if ( $uri ~ /pref/(.*)/ ) {
set $oldseo $1;
}
if ( $newseo !~ "--NOOP--" ) {
rewrite /pref/(.*)/ /pref/$newseo/ permanent;
break;
}
}
}
How it works
Map works that way:
when you set value to first variable in map definition ( in my example $oldseo ) second variable ($newseo) gets corresponding value assigned. So instruction set $oldseo $1; Has an effect of setting $newseo as well.
I wrote that example from information I found on mailing list.
