在Nginx中,如何匹配整个URL和查询字符串以及如何重定向到URL和查询字符串 [英] In Nginx, how to match and entire URL and Query String and redirect to a URL and Query String

查看:1850
本文介绍了在Nginx中,如何匹配整个URL和查询字符串以及如何重定向到URL和查询字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经搜索了一段时间,尝试变通方法,并且没有提出任何有用的信息.

I have been searching for a while now, trying work arounds and haven't come up with anything useful.

我有一个(大)网站迁移中的URL列表,需要匹配整个URL +查询字符串并重定向到另一个URL.

I have a (large) list of URL's from an site migration and need to match the entire URL + Query String and redirect to another URL.

据我所知,以下仅匹配/mens,而不匹配其余的查询字符串.

As far as I can see the following only matches /mens, but not the rest of the query string.

rewrite "^/mens?brand%5B%5D=27&section%5B%5D=5&price-min=0&price-max=2000&sort=newest"  "/t/gender/men" permanent;

重要的原因是我有一堆类似的URL,但查询字符串略有不同,需要重定向,类似于下面的内容,但实际上可以工作....:-/

The reason it's important is that I have a bunch of similar URL's with slightly different Query Strings, which need to be redirected, similar to below, but actually work.... :-/

rewrite "^/mens/shop?q=road+map+polo"       "/t/category/golf-knits"    permanent;
rewrite "^/mens/shop?q=six+pocket+pant"     "/t/category/golf-pants"    permanent;

#etc... ad noiseam 

预先感谢, 保罗.

推荐答案

$request_uri变量包含整个URL.您可以使用map将其转换为重定向.

The $request_uri variable contains the entire URL. You could use a map to translate it into a redirection.

map $request_uri $target {
    ~*^/mens/shop\?q=road\+map\+polo   /t/category/golf-knits;
    ~*^/mens/shop\?q=six\+pocket\+pant /t/category/golf-pants;
}

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

有关详细信息,请参见本文档.

See this document for details.

这篇关于在Nginx中,如何匹配整个URL和查询字符串以及如何重定向到URL和查询字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆