Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RegEx URL ReWrite match all in expression unless term exists

I am currently rewriting a URL, however my RegEx expression is catching a directory I want to be ignored.

The rewrite rule is

^people/([A-Za-z0-9\-\_]+)/?$

...Which catches everything that matches. However, I would like to exclude a directory, People_Search, so for instance...

/people/John_Smith

...will forward, but

/people/People_Search

...should not suppose to be.

That's the only term I want to look for, so if it exists anywhere in the string, I want to ignore it.

Any ideas?

like image 461
George Johnston Avatar asked Feb 27 '23 10:02

George Johnston


2 Answers

Regex has a thing called a "non capturing negative lookahead assertion" which basically says "don't match the following". It looks like this:

^people/(?!People_Search)([A-Za-z0-9\-\_]+)/?$ 

Whether you can use this depends on the rewrite engine you use, and the level of regex support that's included in it. I'd expect that most common rewriters support this.

FYI: There are also negative lookbehind assertions(?<!), and also postive versions of the lookahead (?=) and lookbehind (?<=) assertions.

Tutorial: http://www.regular-expressions.info/lookaround.html

like image 72
Cheeso Avatar answered Mar 10 '23 12:03

Cheeso


^people/(?!People_Search)([A-Za-z0-9\-\_]+)/?$

A negative lookahead to prevent matching People_Search after people/

like image 44
David Kanarek Avatar answered Mar 10 '23 10:03

David Kanarek