Regex to match a URL pattern for a htaccess file

Question!

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: domain.com/group/$groupname
  • New: domain.com/group/$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: domain.com/group/2009-neato-group

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.

Thanks



Answers

Your design choices make a pure-htaccess solution difficult, especially since group/123-another-old-group-456/a-module-inside-the-group/ is a valid old-style URL.

I'd suggest a redirector script that looks at its arguments, determines if the first part is a valid groupid, and if so, it's a new-style URL takes the user to that group. Else, it is a old-style URL, so hand it off to old_group.php. Something like this:

RewriteRule ^group/(.*)$ /redirector.php/$1 [L,QSA]
By : sigint


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.



This video can help you solving your question :)
By: admin