Ryan
Ryan

Reputation: 642

Regex not detect for htaccess Redirect

I'm looking for essentially the opposite of this ...

^(.*)(?=\/nz\/)(.*)$

for some reason

^(.*)(?!=\/nz\/)(.*)$

is not working as I thought it would. I expected it to select like this

https://www.awebsite.com/fg/about-us/our-business -yes
https://www.awebsite.com/xx/about-us/our-business -yes
https://www.awebsite.com/pp/about-us/our-business -yes
https://www.awebsite.com/nz/about-us/our-business -no
/xx/about-us/our-business -yes
/pp/about-us/our-business -yes
/fg/about-us/our-business -yes
/nz/about-us/our-business -no
/about-us/our-business - no (is this also possible with 1 regex statement or do i need multiple rewritecond statements)

Any guidance on getting a solution that selects all lines that don't have /nz/ in them would be much appreciated.

Side-note: this is for a rewrite/redirect in .htaccess.

Upvotes: 1

Views: 63

Answers (1)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 522762

This is the regex I think you should be using:

^((?!\/nz\/).)*$

Explanation:

  • (?!\/nz\/). asserts that what follows each position in the string is not /nz/, and then it matches any character
  • ((?!\/nz\/).)* this expression is then matched any number of times, since the URL can be of arbitrary length

Demo here:

Regex101

Update:

Here is a regex which should meet all of your requirements:

^(?!\/nz\/)\/\w\w(?!\/nz\/)\/((?!\/nz\/).)*$

Upvotes: 1

Related Questions