Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
331 views
in Technique[技术] by (71.8m points)

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...