George Johnston
George Johnston

Reputation: 32258

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?

Upvotes: 4

Views: 5737

Answers (2)

David Kanarek
David Kanarek

Reputation: 12613

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

A negative lookahead to prevent matching People_Search after people/

Upvotes: 1

Cheeso
Cheeso

Reputation: 192487

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

Upvotes: 7

Related Questions