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?
我们正在推出新版本的“群组”功能,但需要支持同一网址上的所有旧群组。幸运的是,这两个功能有不同的url模式,所以这应该很容易?对?
- 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]
我在正则表达式中使用的当前正则表达式将旧组重定向到旧脚本是: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
在一个名为: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.
我想到的最好的方法是它不能以“group-slash-numbers-slash”开头,但却无法将其变成正则表达式。所以,否定这种模式可能会起作用。
Thanks
2 个解决方案
#1
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.
反过来说:如果网址匹配^ group / [0-9] + /。+,则它是一个新组,因此首先重定向这些(使用[L]规则)。其他一切都必须是一个老组,你可以保留旧的规则。
#2
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.
您的设计选择使纯粹的htaccess解决方案变得困难,尤其是因为group / 123-another-old-group-456 / a-module-the-group /是一个有效的旧式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:
我建议一个重定向器脚本查看其参数,确定第一部分是否是一个有效的groupid,如果是,它是一个新式的URL将用户带到该组。否则,它是一个旧式URL,所以将其交给old_group.php。像这样的东西:
RewriteRule ^group/(.*)$ /redirector.php/$1 [L,QSA]
#1
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.
反过来说:如果网址匹配^ group / [0-9] + /。+,则它是一个新组,因此首先重定向这些(使用[L]规则)。其他一切都必须是一个老组,你可以保留旧的规则。
#2
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.
您的设计选择使纯粹的htaccess解决方案变得困难,尤其是因为group / 123-another-old-group-456 / a-module-the-group /是一个有效的旧式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:
我建议一个重定向器脚本查看其参数,确定第一部分是否是一个有效的groupid,如果是,它是一个新式的URL将用户带到该组。否则,它是一个旧式URL,所以将其交给old_group.php。像这样的东西:
RewriteRule ^group/(.*)$ /redirector.php/$1 [L,QSA]