Nginx: How to transfer a permanent redirect from this list?

I have about 400 URLs that will change in the new version, and for some reason I cannot repeat the same type of URL structure on the new website.

My question is: can I give a list of URLs for nginx (yes, I know 400) and tell him simply that each of them is going to a different URL?

As I said, the structure of the URL will be different, so I can not use any pattern.

Thanks in advance.

+5
source share
2 answers

If you have a very long list of entries, it might be a good idea to leave them outside the nginx configuration file:

map_hash_bucket_size 256; # see http://nginx.org/en/docs/hash.html map $request_uri $new_uri { include /etc/nginx/oldnew.map; #or any file readable by nginx } server { listen 80; server_name your_server_name; if ($new_uri) { return 301 $new_uri; } ... } 

/etc/nginx/oldnew.map :

 /my-old-url /my-new-url; /old.html /new.html; 

Be sure to end each line with a ";" char!

Also, if you need to redirect all URLs to another host, you can use:

 return 301 http://example.org$new_uri; 

Or, if you also need to redirect to another port:

 return 301 http://example.org:8080$new_uri; 
+8
source

Probably the easiest way to do this is to wrap map in your list. The configuration in this case will look like this:

 map $request_uri $new_uri { default ""; /old/page1.html /new/page1.html; /old/page2.html /new/page2.html; ... } server { ... if ($new_uri != "") { rewrite ^(.*)$ $new_uri permanent; } ... } 
+6
source

Source: https://habr.com/ru/post/1216483/


All Articles