I worked out a solution so that I just use the back refs $1 and $3 ignoring $2 whether it is matched or null.
rewrite ^/somefolder(.*)(asdf)(.*html)$ http://example.com$1$3 permanent;
문제
I want to optionally match a string (asdf) and remove it from a rewrite.
location ~ /somefolder {
rewrite ^/somefolder(.*)(?:asdf)?(.*html)$ http://example.com$1$2 permanent;
}
So this would rewrite the requested url to the root domain as well as strip the url of any occurrence of asdf
if present.
Below the address mydomain.com/somefolder/pencil.html
is rewritten to 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"
Where as here mydomain.com/somefolder/pencil-asdf.html
is rewritten to 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"
Shouldn't asdf
be getting stripped using the (?:asdf)
match yielding mydomain.com/pencil-.html
해결책
I worked out a solution so that I just use the back refs $1 and $3 ignoring $2 whether it is matched or null.
rewrite ^/somefolder(.*)(asdf)(.*html)$ http://example.com$1$3 permanent;