Nginx Regex和可选的backreference

Nginx Regex和可选的backreference,regex,nginx,pcre,backreference,Regex,Nginx,Pcre,Backreference,我希望有选择地匹配字符串(asdf)并将其从重写中删除 location ~ /somefolder { rewrite ^/somefolder(.*)(?:asdf)?(.*html)$ http://example.com$1$2 permanent; } 因此,这将把请求的url重写到根域,并删除url中出现的asdf(如果存在) 下面的地址mydomain.com/somefolder/pencil.html被重写为mydomain.com/pencil.html 2013/

我希望有选择地匹配字符串(asdf)并将其从重写中删除

location ~ /somefolder {
    rewrite ^/somefolder(.*)(?:asdf)?(.*html)$ http://example.com$1$2 permanent;
}
因此,这将把请求的url重写到根域,并删除url中出现的
asdf
(如果存在)

下面的地址
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"
其中as here
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
不应该使用
(?:asdf)
匹配来剥离吗
mydomain.com/pencil-.html

我制定了一个解决方案,这样我就可以使用back refs$1和$3忽略$2,不管它是匹配的还是空的

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"
rewrite ^/somefolder(.*)(asdf)(.*html)$ http://example.com$1$3 permanent;

你应该为你的问题添加一个答案并接受它——如果人们有同样的问题,这将使他们更容易找到解决方案。