使用 !? 时重写规则不传递 $1

发布于 2024-09-11 19:49:49 字数 492 浏览 2 评论 0原文

我正在使用这条规则:(

RewriteRule !^(.*?/.*|.*?\.(?:php|html)$)$ headers.php?a=$1 [L]

基于 正则表达式匹配这个或那个?)

当我输入 localhost/fooa 变量为空而不是 foo 时,它会重写到 headers.php > (我检查了 var_dump($_REQUEST)

知道为什么吗?我尝试使用

RewriteCond  %{REQUEST_URI}  !headers

但事实并非如此。

谢谢你!

I am using this rule:

RewriteRule !^(.*?/.*|.*?\.(?:php|html)$)$ headers.php?a=$1 [L]

(based on the great contributions on Regex match this OR that?)

It rewrites to headers.php when I type localhost/foo but the a variable is empty instead of foo (I checked with var_dump($_REQUEST))

Any idea why? I tried using

RewriteCond  %{REQUEST_URI}  !headers

but it wasn't that.

Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

看春风乍起 2024-09-18 19:49:49

该规则被否定,因此当且仅当正则表达式与正在处理的 URI 不匹配时才会执行该规则。由于捕获组与 localhost/foo 不匹配,因此正则表达式引擎无法将任何内容放入 $1 中。解决方案是避免在 RewriteRule 指令中使用否定,而是使用 RewriteCond 指令来检查否定的正则表达式。以下规则集应该有效。 (不过我还没有测试过。可能某个地方有错误。)

RewriteCond %{REQUEST_URI} !/.*/
RewriteCond %{REQUEST_URI} !\.(html|php)$
RewriteRule (.*) headers.php?a=$1 [L]

The rule is negated, so it is executed if and only if the regular expression doesn't match the URI being processed. Since the capturing group doesn't match localhost/foo, there's nothing for the regex engine to put into $1. The solution is to avoid the use of negation in your RewriteRule directive, and instead use RewriteCond directives to check the negated regex. The following ruleset should work. (I haven't test it, though. It's possible that there's a mistake somewhere.)

RewriteCond %{REQUEST_URI} !/.*/
RewriteCond %{REQUEST_URI} !\.(html|php)$
RewriteRule (.*) headers.php?a=$1 [L]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文