0

我想选择匹配一个字符串(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"

在这里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

4

1 回答 1

0

我制定了一个解决方案,以便我只使用后面的参考 $1 和 $3 忽略 $2 是否匹配或为空。

rewrite ^/somefolder(.*)(asdf)(.*html)$ http://example.com$1$3 permanent;
于 2014-03-29T04:53:43.960 回答