0

I post here even if its probably more a SF question I think it could interest some of you.

Is there a way to tell with Regex to nginx if, for example, the 3rd catpure groupe is match redirect to that else to that ?

Example : We have 2 types of URL :

circuit/asie/indonesie/denpasar-bali/ or circuit/asie/indonesie/

and we want to redirect to

asie/indonesie/denpasar-bali/ or asie/indonesie/

Let say we have this Regex : \w+\/([^\/]+)\/([^\/]+)\/?([^\/]+)?\/

Playground here

How do you say if match the first case redirect to the first url and match the second redirect to the second ?

Does something like this could work ?

rewrite    \w+/([^/]+)/([^/]+)/?([^/]+)?/     /$1/$2/?$3?/ permanent;

Baldráni
  • 5,332
  • 7
  • 51
  • 79

1 Answers1

0

You can try this

rewrite ^(?:.*?/)(.+)$ $1 redirect;

For capturing after n th group, you can use

^(?:(?:.*?\/){n})(.+)$

Regex Demo

If you want to use allowed characters in URL name, you can find the answer here

Community
  • 1
  • 1
rock321987
  • 10,942
  • 1
  • 30
  • 43