Please consider the following URLs.

  1. https://products.example.com/family/child
  2. https://products.example.com/products/family/child
  3. https://products.example.com/family/children
  4. https://products.example.com/products/family/children

Among the four, I need to redirect one as follow: https://products.example.com/family/children

Which means that I want to redirect the URLs that do not contain the word /products/ and /children. If the URL does not contain the word /products/ but contain /child at the end, I need to replace the /child with /children.

UPDATE 1: I have 2 problems as below.

  1. Identify the URL that are to be substituted.
  2. Modify the identified URL as required.

I can identify the URL with this regex but not able to substitute.

Update 2: I think I found a way to identify: ^(?!.*\/products(?:\/))(?:(.*)\/|$)?(?:child)$ and substitute: $1/children

Next problem is: the regex works with option /m only. Can this be modified to work without it as:

rewrite ^(?!.*\/products(?:\/))(?:(.*)\/|$)?(?:child)$ $1/children permanent;

1 Answer 1


Something like this should work for what you've said above, however your question isn't as precise as it could be and this might not be exactly what you want.

The = means exact match. Remove it and I think you get a substring match. You'll can read the documentation for details.

location = /family/child {
  return 301 http://www.example.com/family/children;
  • Please check the update 2 from my question. I am able to identify and substitute the URL, however, it is working with /m (multiline) switch. Problem is, I do not have testing environment for troubleshooting so I need to make sure that the regex and rewrite will work on production. Could you please have a look?
    – Prorata
    Mar 29, 2018 at 5:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .