可选的 RewriteRule 参数
如何将这些 RewriteRules 合并到一行中,并且每个参数都是可选的?
RewriteRule ^(.*)/(.*)/(.*)/(.*)/ index.php?_page=$1&_sub=$2&_area=$3&_ex=$4 [NC,QSA,L]
RewriteRule ^(.*)/(.*)/(.*)/ index.php?_page=$1&_sub=$2&_area=$3 [NC,QSA,L]
RewriteRule ^(.*)/(.*)/ index.php?_page=$1&_sub=$2 [NC,QSA,L]
RewriteRule ^(.*)/ index.php?_page=$1 [NC,QSA,L]
RewriteRule ^ index.php [NC,QSA]
我相信 .*
自动需要下一个块,否则它也会被捕获,因此您可以将其替换为 [^/]+
但是当我在一行上执行此操作时(删除除了第一条规则之外的所有规则),它停止工作了吗?
How can I combine these RewriteRules into one single line, with each parameter being optional?
RewriteRule ^(.*)/(.*)/(.*)/(.*)/ index.php?_page=$1&_sub=$2&_area=$3&_ex=$4 [NC,QSA,L]
RewriteRule ^(.*)/(.*)/(.*)/ index.php?_page=$1&_sub=$2&_area=$3 [NC,QSA,L]
RewriteRule ^(.*)/(.*)/ index.php?_page=$1&_sub=$2 [NC,QSA,L]
RewriteRule ^(.*)/ index.php?_page=$1 [NC,QSA,L]
RewriteRule ^ index.php [NC,QSA]
I believe that .*
automatically requires the next block otherwise it's captured too, so you can replace that with [^/]+
however when I do that on one line (removing all but the first rule), it stops working?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可能会感到失望,因为您所要求的问题没有解决方案。
此外,对于每个(n+1)子目录,变量名称都会改变......所以肯定没有解决方案。
无论如何,您可以使用以下方法来减少 RewriteRules:
这使得第二个参数为空但匹配一个或两个参数。
You may be disappointed because there's no solution of what you're asking for.
Moreover for each (n+1) subdir the variable name changes... so there's definitely no solution.
Anyway you can reduce your RewriteRules using a thing like:
which makes the second argument empty but matches both one or two arguments.
你的 rexexp
^app/?([\w]+)?/?([\w]+)?/?([\w]+)?/?((.*?)+)?$< /code> 在语义上与您的起始级联不同。
无论如何,在正则表达式中,贪婪的
([^/]*)
更好,因为这可以与/?
一起正常工作。Your rexexp
^app/?([\w]+)?/?([\w]+)?/?([\w]+)?/?((.*?)+)?$
is semantically different to your starting cascade.Anyway in regexp terms the greedy
([^/]*)
is better as this will work properly with a/?
.