Regex to match a URL pattern for a htaccess file


I'm really struggling with this regex.

We're launching a new version of our "groups" feature, but need to support all the old groups at the same URL. Luckily, these two features have different url patterns, so this should be easy? Right?

  • Old:$groupname
  • New:$groupid/$groupname

The current regex I was using in the regex to redirect old groups to the old script was: RewriteRule ^(group/([^0-9/].*))$ /old_group.php/$1 [L,QSA]

It was working great until a group popped up called something like:

At which point the regex matched and forced the new group to the old code because it contained numbers. Ooops.

So, any thoughts on a regex that would match the old pattern so I can redirect it?

Patterns it must match:

  • group/my-old-group
  • group/another-old-group/
  • group/123-another-old-group-456/a-module-inside-the-group/

Patterns it cannot match:

  • group/12/a-new-group
  • group/345/another-new-group/
  • group/6789/a-3rd-group/module-abc/

The best way I've thought about it is that it cannot start with "group-slash-numbers-slash", but haven't been able to turn that into a regex. So, something to negate that pattern would probably work.


Best Solution

Think the other way around: If the url matches ^group/[0-9]+/.+, then it is a new group, so redirect these first (with a [L] rule). Everything else must be a old group, you can keep your old rules there.

Related Question