用于匹配 htaccess 文件的 URL 模式的正则表达式

发布于 2024-07-27 18:51:17 字数 940 浏览 5 评论 0原文

我真的很挣扎于这个正则表达式。

我们正在推出新版本的“群组”功能,但需要支持同一 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

对不⑦ 2024-08-03 18:51:18

换个角度思考:如果 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.

独自唱情﹋歌 2024-08-03 18:51:18

您的设计选择使 pure-htaccess 解决方案变得困难,特别是因为 group/123-another-old-group-456/a-module-inside-the-group/ 是有效的旧式 URL。

我建议使用一个重定向器脚本来查看其参数,确定第一部分是否是有效的groupid,如果是,则它是一个将用户带到该组的新型 URL。 否则,它是一个旧式 URL,因此将其交给 old_group.php。 像这样的事情:

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

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]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文