Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nginx: How to mass permanent redirect from a given list?

Tags:

I have about 400 url that will change in the new version and for some reasons I can't repeat the same type of url structure in the new website.

My question is, can I give a url list to nginx (yeah I know the 400 ones), and tell him simply that each one of them are going to another url?

Like I said the url structure will be different so I can't use any type of pattern.

Thanks in advance.

like image 224
Raphael Leroux Avatar asked Mar 30 '15 19:03

Raphael Leroux


People also ask

What is NGINX permanent redirect?

Permanent redirects such as NGINX 301 Redirect simply makes the browser forget the old address entirely and prevents it from attempting to access that address anymore. These redirects are very useful if your content has been permanently moved to a new location, like when you change domain names or servers.

What is return 301 in NGINX?

Visitor–> Website Page–> Website is under maintenance. On the other hand, a permanent Nginx redirect informs the web browser that it should permanently link the old page or domain to a new location or domain. To map this change, the redirects response code 301 is used for designating the permanent movement of a page.


1 Answers

If you have a very long list of entries, could be a good idea to keep them outside of 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 sample:

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

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

Moreover, if you need to redirect all URLs to another host, you could 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; 
like image 132
lifeisfoo Avatar answered Sep 16 '22 16:09

lifeisfoo