多次匹配 .htaccess 上的正则表达式
我又来寻求你的帮助了。我现在有一个巨大的 .htaccess 文件,我们用它来美化我们的 URL。这通常非常简单,但我现在遇到了一个问题,我不知道如何解决。如果是 PCRE,我知道我可以简单地执行 foreach 来处理它,但在 .htaccess 中我不知道如何继续。
我的 URl 可以有一组关于页面上分隔块的页面 id,这是我用来匹配它的代码 (?:/((?:glc|gli[0-9]+)?page )/([0-9]+))
并将其添加到我的 [L,QSA] 规则中,如 $1=$2
(表达式比它复杂得多,我'我只是简单地获取相关部分)。
通常,pageid 可能出现在 url 上,也可能不出现,在这种情况下,我只需在其末尾添加一个 ?
,但在这个特定的 URL 上,我最多可以有 28 个这样的代码。我可以向匹配模式添加 *
或更具体的 {0,28}
,但我不知道如何在返回部分引用每一对。
简而言之,我想转换:
/page/10/glcpage/1/gli123/9 => page=10&glcpage=1&gli123page=9
/page/5 => page=5
等
有人知道吗?
对于那些足够虐待狂的人来说,这是完整的正则表达式。 pageid=$4 是当 URL 上只有 1 种可能性时抓取页面的正常代码。这就是我正在努力改变的。
^modsystem/([0-9]+)(?:/(?!(?:glc|gli[0-9]+)?page/[0-9]+|comment/[0-9]+|module/[0-9]+)([^/]+))?(?:/module/([0-9]+))?(?:/((?:glc|gli[0-9]+)?page)/([0-9]+))*(?:/comment/([0-9]+))?
/modsystem.php?action=showall&modsystemid=$1&seoslug=$2&moduleid=$3&pageid=$4&commentid=$5
[L,QSA]
编辑:修复一些错误复制的括号
I come again for your help. I have right now a huge .htaccess file that we use to prettify our URLs. This is usually pretty straightforward, but I got on an issue now that I don't know how to solve. Were it PCRE I knew I could simply do a foreach to process this, but in .htaccess I do not know how to proceed.
My URl can have a set of page ids regarding separated blocks on the page, this is the code I'm using to match it (?:/((?:glc|gli[0-9]+)?page)/([0-9]+))
and I add it to my [L,QSA] rule as $1=$2
(the expression is much more complex than it, I'm simply getting the relevant part).
Normally a pageid may appear or not on an url, and on those cases I simply add a ?
at the end of it, but on this particular URL I may have up to 28 of these. I could add a *
or a more specific {0,28}
to the match pattern, but I have no idea how to reference each pair on return part.
Briefly, I want to convert:
/page/10/glcpage/1/gli123/9 => page=10&glcpage=1&gli123page=9
/page/5 => page=5
etc
Anyone got any idea?
For those sadistic enough this is the full regexp. The pageid=$4 is the normal code for grabbing the page when there is only 1 possibility on the url. That's what I'm trying to change.
^modsystem/([0-9]+)(?:/(?!(?:glc|gli[0-9]+)?page/[0-9]+|comment/[0-9]+|module/[0-9]+)([^/]+))?(?:/module/([0-9]+))?(?:/((?:glc|gli[0-9]+)?page)/([0-9]+))*(?:/comment/([0-9]+))?
/modsystem.php?action=showall&modsystemid=$1&seoslug=$2&moduleid=$3&pageid=$4&commentid=$5
[L,QSA]
EDIT: Fixing some parenthesis that got wrongly copied
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须单独测试每个可能的 pageid。无法循环匹配或处理可变数量的匹配。
You'll have to test individually for each possible pageid. There's no way to loop through the matches or handle a variable number of matches.