Nginx redirecting for specific list of arguments

梦想与她 提交于 2019-12-11 05:56:49

问题


I have a list of some obsolete arguments in URLs.

arg1
arg2
arg3
...
argn

I need to redirect any of the request having any of those arguments in the query part of the link, to a specific site.

/page.html?arg1=sxx&arg2=uuu  -> http://xxx.x.xxx.x
/page.php?arg3=  -> http://xxx.x.xxx.x
/dir/dir/page/?arg2=111&& argn=xyyyy  -> http://xxx.x.xxx.x
/page.html (is not redirected but matched to other existing rules in nginx)

Any idea how to express it nicely? For some reasons location has no arguments to be matched by regular expression.


回答1:


Assuming that the argument order is deterministic, you can test multiple regular expressions against the $request_uri variable.

The map directive can be used to list multiple rules and destinations.

For example:

map $request_uri $redirect {
    default                          0;
    ~^/page\.html\?arg1=sxx&arg2=uuu http://xxx.x.xxx.x;
    ~^/page\.php\?arg3=              http://xxx.x.xxx.x;
    ...
}

server {
    ...
    if ($redirect) {
        return 301 $redirect;
    }

You will of course want to improve the regular expressions above, by inserting gaps (.*) and word boundaries (\b).

See this document for the map directive, and this note on the use of if.



来源:https://stackoverflow.com/questions/42843692/nginx-redirecting-for-specific-list-of-arguments

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!