Home > OS >  Regex to redirect all traffic to new path except ending with specified string
Regex to redirect all traffic to new path except ending with specified string

Time:05-05

I am trying to redirect all traffic from wwww.mydomain.com/site/ to wwww.mydomain.com except for the URLs which contain /events/ at the end. I am using the following regex right now which caters to most of the cases:
Source: ^/cra/(?!events)(.*)
Target: /$1
It would redirect all traffic except the ones that contains events as string in URL, but the problem with this regex is, it's not redirecting wwww.mydomain.com/site/events/myevent to wwww.mydomain.com/events/myevent I couldn't find something that would address mentioned use-case as well, would be great if anyone could help me out.
enter image description here

CodePudding user response:

If I understand your problem correctly, you want to redirect everything from

/site/*

to

/

BUT: if it contains

/events/

at the very end: do not redirect at all

If that is what you are trying to do, this should work.

^(?!.*(events\/\Z))(\/site\/)(.*)

Access to part to keep after the Redirection with $3.

Tip: regex101 is a really neat site to figure out and play with regex.

Note that this only works if the "event/" part has a trailing '/'. But you can work from there I suppose.

Edit: Adjusted for Zohaib's comment

  • Related