How to redirect single url in nginx?

前端 未结 3 1972
我在风中等你
我在风中等你 2020-12-02 05:09

I\'m in the process of reorganizing url structure. I need to setup redirect rules for specific urls - I\'m using NGINX.

Basically Something like this:

<         


        
3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-02 05:25

    If you need to duplicate more than a few redirects, you might consider using a map:

    # map is outside of server block
    map $uri $redirect_uri {
        ~^/issue1/?$    http://example.com/shop/issues/custom_isse_name1;
        ~^/issue2/?$    http://example.com/shop/issues/custom_isse_name2;
        ~^/issue3/?$    http://example.com/shop/issues/custom_isse_name3;
        # ... or put these in an included file
    }
    
    location / {
        try_files $uri $uri/ @redirect-map;
    }
    
    location @redirect-map {
        if ($redirect_uri) {  # redirect if the variable is defined
            return 301 $redirect_uri;
        }
    }
    

提交回复
热议问题