用于匹配 htaccess 文件的 URL 模式的正则表达式
我真的很挣扎于这个正则表达式。
我们正在推出新版本的“群组”功能,但需要支持同一 URL 上的所有旧群组。 幸运的是,这两个功能有不同的 url 模式,所以这应该很容易? 正确的?
- 旧:domain.com/group/$groupname
- 新:domain.com/group/$groupid/$groupname
我在正则表达式中使用的当前正则表达式将旧组重定向到旧脚本是:RewriteRule ^(group/ ([^0-9/].*))$ /old_group.php/$1 [L,QSA]
一直运行良好,直到出现一个名为 domain.com/group/ 的群组2009-neato-group
此时正则表达式匹配并强制新组使用旧代码,因为它包含数字。 哎呀。
那么,对于与旧模式匹配的正则表达式有什么想法,以便我可以重定向它?
它必须匹配的模式:
- group/my-old-group
- group/another-old-group/
- group/123-another-old-group-456/a-module-inside-the-group/
< strong>它无法匹配的模式:
- group/12/a-new-group
- group/345/another-new-group/
- group/6789/a-3rd-group/module-abc/
我最好的方法我想到的是它不能以“group-slash-numbers-slash”开头,但无法将其转换为正则表达式。 因此,否定这种模式的方法可能会起作用。
谢谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
换个角度思考:如果 url 匹配 ^group/[0-9]+/.+,那么它是一个新组,因此首先重定向它们(使用 [L] 规则)。 其他一切都必须是旧的组,你可以保留旧的规则。
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.
您的设计选择使 pure-htaccess 解决方案变得困难,特别是因为
group/123-another-old-group-456/a-module-inside-the-group/
是有效的旧式 URL。我建议使用一个重定向器脚本来查看其参数,确定第一部分是否是有效的
groupid
,如果是,则它是一个将用户带到该组的新型 URL。 否则,它是一个旧式 URL,因此将其交给old_group.php
。 像这样的事情: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 toold_group.php
. Something like this: