问题
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