如何从url NGINX

时间:2017-10-05 15:40:58

标签: redirect nginx url-rewriting nginx-location

我有这个网址xxxxx.com/xxxx/xx-xx-2/xxxx,并希望从网址中的第二个参数中删除“-2”并保持这种方式xxxxx.com/xxxx/xx-xx/ xxxx,“ - 2”是随机的。我不知道该怎么做:(任何帮助都会很感激。

我为我糟糕的英语道歉

1 个答案:

答案 0 :(得分:0)

您应该在下面添加

location ~ /[^/]+/[^/]+-[\d+]/ {
      rewrite ^/([^/]+/[^/]+)-[\d+]/(.*) $1/$2 redirect;
}

在网址/part1/part2-number/ /[^/]+匹配/part1/[^/]+匹配/part2-[\d+]/匹配-number,然后重写它以删除数字

测试结果:

$ curl -I localhost/tarun/lalwani-2/abc
HTTP/1.1 302 Moved Temporarily
Server: openresty/1.11.2.2
Date: Fri, 06 Oct 2017 11:44:23 GMT
Content-Type: text/html
Content-Length: 167
Location: http://localhost/tarun/lalwani/abc
Connection: keep-alive

$ curl -I localhost/tarun/lalwani-10/abc
HTTP/1.1 302 Moved Temporarily
Server: openresty/1.11.2.2
Date: Fri, 06 Oct 2017 11:44:27 GMT
Content-Type: text/html
Content-Length: 167
Location: http://localhost/tarun/lalwani/abc
Connection: keep-alive