0

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

mhumesf
  • 421
  • 2
  • 10
  • 1
    You should add an answer to your question and accept it - will make it easier for people to work out that there is a solution, if they have the same problem. – Dave Child Feb 25 '14 at 10:24

1 Answers1

0

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;
duncan
  • 29,669
  • 9
  • 70
  • 93
mhumesf
  • 421
  • 2
  • 10