Negate Route Patterns

Hi all,

I’m trying to write a route that is a negative of a pattern but I can’t get it to work, so I’m hoping for some tips.

So, I have this working fine:

$app->group(('/{locale:en-gb|en-us|fr-fr}', function() {

Within this group I have individual routes that are available for these locales. What I need to do is to have a route that doesn’t match these locales so that I can redirect to the browser’s preferred locale. If, once we get into that group, we still can’t match a route, then I want to 404.

So I’m testing this on Regex101 and this works to match URLs link:


(I’m using ` as the delimiter)

Sample URLs I’m testing:


I can negate it with this regex link:


But I can’t get that to work as a route. Any ideas?

I’ve never tried to do a negative match so I can’t comment on that part, but since it uses the first match, could you put the redirect and the very end matching everything and then make your last route in the group the 404 (also matching everything)? Kind of like an else.

I did consider using the 404 rules to redirect, but I don’t want it to always redirect if the first segment isn’t an enabled locale. If I did that then we could be redirecting forever :slight_smile: