【发布时间】:2013-09-16 06:03:36
【问题描述】:
我想选择性地匹配一个字符串 (asdf) 并将其从重写中删除。
location ~ /somefolder {
rewrite ^/somefolder(.*)(?:asdf)?(.*html)$ http://example.com$1$2 permanent;
}
因此,这会将请求的 url 重写为根域,并删除任何出现 asdf 的 url(如果存在)。
下面地址mydomain.com/somefolder/pencil.html改写为mydomain.com/pencil.html
2013/09/16 09:52:24 [notice] 16866#0: *1 "^/somefolder(.*)(?:asdf)?(.*html)$" matches "/somefolder/pencil.html", client: 10.0.0.2, server: mydomain.com, request: "GET /somefolder/pencil.html HTTP/1.1", host: "mydomain.com"
2013/09/16 09:52:24 [notice] 16866#0: *1 rewritten redirect: "http://mydomain.com/pencil.html", client: 10.0.0.2, server: mydomain.com, request: "GET /somefolder/pencil.html HTTP/1.1", host: "mydomain.com"
这里mydomain.com/somefolder/pencil-asdf.html被重写为mydomain.com/pencil-asdf.html
2013/09/16 09:52:31 [notice] 16866#0: *1 "^/somefolder(.*)(?:asdf)?(.*html)$" matches "/somefolder/pencil-asdf.html", client: 10.0.0.2, server: mydomain.com, request: "GET /somefolder/pencil-asdf.html HTTP/1.1", host: "mydomain.com"
2013/09/16 09:52:31 [notice] 16866#0: *1 rewritten redirect: "http://mydomain.com/pencil-asdf.html", client: 10.0.0.2, server: mydomain.com, request: "GET /somefolder/pencil-asdf.html HTTP/1.1", host: "mydomain.com"
不应该使用(?:asdf) 匹配产生mydomain.com/pencil-.html 来剥离asdf
【问题讨论】:
-
您应该为您的问题添加一个答案并接受它 - 如果他们有同样的问题,这将使人们更容易找到解决方案。
标签: regex nginx pcre backreference