Nginx config for redirect map
Using 'Map' for redirects
map_hash_bucket_size 128; map $uri $new_uri { /test-to-rewrite /test-rewritten; /test2-to-rewrite /test2-rewritten; } server { server_name example.com; if ($new_uri) { return 301 $new_uri; } #... }
Source: https://www.dogsbody.com/blog/nginx-optimising-redirects/
Another example for using query strings:
http {
map_hash_bucket_size 128;
map $uri$is_args$args $new_uri {
#/hello?123 /hello123?345;
include /my-location/redirect.tsv;
default $uri$is_args$args; # Default to the original URI if not matched
}
server {
location / {
if ($new_uri != $uri$is_args$args) {
return 302 $new_uri;
}
}
}
File: redirect.tsv
/hello?123 /hello123?345;
Comments
Post a Comment