为什么 url rewrite 无法捕获我之前在组中使用 lookahead 的最后一组?

Why url rewrite can't capture my last group with lookahead in the group before?

我试着重写这个URL。第 4 组是可选的,直到问号为止。即使没有 g4 或斜杠,也必须捕获第 5 组。必须在没有第 3 组的情况下捕获第 4 组。

https://www.example.de/prefix/group1/group2/group3-suffix/group4(optional)?group5

Regex 似乎在大多数 regex 变种中都能完全按照需要工作,但在我的 .htaccess 中,第 5 组总是空的。

我的重写是这样的:

RewriteRule ^prefix/([^/]*)/([^/]*)/([^/]*)-suffix?/?((?:(?![?/]).)*)/?\??(.*)?/?$ /target.jsp?g1=&g2=&g3=&g4=& [L]

感谢您的帮助!

由于 RewriteRule 不查看查询字符串,您需要使用预定义标志将旧查询字符串附加到目标 URL,称为 QSA

RewriteRule ^prefix/([^/]*)/([^/]*)/([^/]*)-suffix?/?([^/]*)/?$ /target.jsp?g1=&g2=&g3=&g4= [L,QSA]