可选的 RewriteRule 参数

发布于 2024-12-25 21:55:45 字数 521 浏览 0 评论 0原文

如何将这些 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 技术交流群。

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

发布评论

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

评论(2

叫思念不要吵 2025-01-01 21:55:45

您可能会感到失望,因为您所要求的问题没有解决方案。

此外,对于每个(n+1)子目录,变量名称都会改变......所以肯定没有解决方案。

无论如何,您可以使用以下方法来减少 RewriteRules:

RewriteRule ^([^/]*)(/([^/]*))?/ index.php?_page=$1&_sub=$2 [NC,QSA,L]

这使得第二个参数为空但匹配一个或两个参数

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:

RewriteRule ^([^/]*)(/([^/]*))?/ index.php?_page=$1&_sub=$2 [NC,QSA,L]

which makes the second argument empty but matches both one or two arguments.

情场扛把子 2025-01-01 21:55:45

你的 rexexp ^app/?([\w]+)?/?([\w]+)?/?([\w]+)?/?((.*?)+)?$< /code> 在语义上与您的起始级联不同。

  • 带有可选 / 的应用程序从哪里来?
  • 您希望如何处理“.”等非单词字符?
  • 非贪婪运算符 ([\w]+)?/ 是多余的
  • 附加参数伪目录呢?

无论如何,在正则表达式中,贪婪的 ([^/]*) 更好,因为这可以与 /? 一起正常工作。

RewriteRule ^([^/]*)/?^([^/]*)/?^([^/]*)/?^([^/]*)$ index.php?_page=$1&_sub=$2_area=$3&_ex=$4 [NC,QSA,L]

Your rexexp ^app/?([\w]+)?/?([\w]+)?/?([\w]+)?/?((.*?)+)?$ is semantically different to your starting cascade.

  • Where did the app with the optional / come from?
  • How do you wan to process non-word characters such as "."?
  • The non-greedy operator ([\w]+)?/ is redundant
  • What about additional parameters pseudo-directories?

Anyway in regexp terms the greedy ([^/]*) is better as this will work properly with a /?.

RewriteRule ^([^/]*)/?^([^/]*)/?^([^/]*)/?^([^/]*)$ index.php?_page=$1&_sub=$2_area=$3&_ex=$4 [NC,QSA,L]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文