Apache重写冲突

发布于 2024-09-15 21:58:20 字数 1598 浏览 2 评论 0原文

我知道有些事情是冲突的,但作为一个模组重写麻瓜,是时候寻求帮助了。

我正在重写 http://test.com/[name]/
http://test.com/script.php?id=[name]
成功后,使用:

(rule#1)
RewriteRule ^(.*)\/$ script.php?id=$1&%{QUERY_STRING} [L]

接下来,我重写 http://test.com/[name] /foobar/[键]
http://test.com/script.php? id=[名称]&foobar=[键]
也成功了,使用:

(rule#2)
RewriteRule ^(.*)\/foobar/([0-9]+)$ script.php?id=$1&foobar=$2 [L]

但是,当我尝试包含查询字符串时,
这样 http://test.com/[name]/ foob​​ar/[key]?p=[页]
可以重写为 http://test.com/script.php?id=[名称]&foobar=[key]&p=[页面]
这样:

(rule#3)
RewriteRule ^(.*)\/foobar/([0-9]+)$ script.php?id=$1&foobar=$2&%{QUERY_STRING} [L]

然后Apache就把我扔回http://test.com/script.php

从反复试验中我知道,如果规则#3 不使用与规则#1 相同的 script.php,则规则#3 会起作用,
但我不知道如何解决它,所以任何帮助表示赞赏!

啊..在用 Gumbo 的示例仔细检查我的规则后,我终于发现出了问题。虽然并不完美,但我的重写规则实际上按预期工作。然而我完全忘记了我有另一个脚本来检查有效参数......显然,抛出随机测试值有时不是一个好主意。不过还是谢谢你的^/技巧!

I know something's conflicting but being a mod rewrite Muggle it's time to ask for help.

I am re-writing http://test.com/[name]/
to http://test.com/script.php?id=[name]
with success, using:

(rule#1)
RewriteRule ^(.*)\/$ script.php?id=$1&%{QUERY_STRING} [L]

Next, I rewrite http://test.com/[name]/foobar/[key]
to http://test.com/script.php?id=[name]&foobar=[key]
also with success, using:

(rule#2)
RewriteRule ^(.*)\/foobar/([0-9]+)$ script.php?id=$1&foobar=$2 [L]

However, when I try to include the query strings,
so that http://test.com/[name]/foobar/[key]?p=[page]
could be re-written to http://test.com/script.php?id=[name]&foobar=[key]&p=[page]
with this:

(rule#3)
RewriteRule ^(.*)\/foobar/([0-9]+)$ script.php?id=$1&foobar=$2&%{QUERY_STRING} [L]

then Apache just throws me back to http://test.com/script.php.

I know from trial and error that rule#3 would work if it did not use the same script.php as rule#1,
but I have no idea how to fix it, so any help appreciated!

Arrgh..after double-checking my rules with Gumbo's examples, I finally found out what went wrong. Although not perfect, my rewrite rules were actually working as intended. However I totally forget that I had another script that checks for valid parameters... apparently, throwing random test values, sometimes is not a good idea. Thanks for the ^/ trick though!

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

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

发布评论

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

评论(2

奢望 2024-09-22 21:58:20

使用 [^/]+ 仅匹配除 / 之外的一个或多个任意字符,而不是 .* 任意数量的任意字符:

RewriteRule ^([^/]+)/$ script.php?id=$1&%{QUERY_STRING} [L]
RewriteRule ^([^/]+)/foobar/([0-9]+)$ script.php?id=$1&foobar=$2 [L]

此外,您可以设置 QSA 标志,将原始请求的查询自动附加到新 URL,而不是手动附加:

RewriteRule ^([^/]+)/$ script.php?id=$1 [L,QSA]

Use [^/]+ to match only one or more arbitrary characters except / instead of .* any number of arbitrary characters:

RewriteRule ^([^/]+)/$ script.php?id=$1&%{QUERY_STRING} [L]
RewriteRule ^([^/]+)/foobar/([0-9]+)$ script.php?id=$1&foobar=$2 [L]

Additionally, you can set the QSA flag to have the original requested query automatically appended to the new URL instead of appending it manually:

RewriteRule ^([^/]+)/$ script.php?id=$1 [L,QSA]
魔法少女 2024-09-22 21:58:20

警告:您正在使用 .*,它实际上无法匹配任何内容。使用 .+ 除非您打算不匹配任何内容。

这里列出了您的所有规则吗?您的 error_log 中是否有任何与此相关的错误?

Warning: you are using .*, which can actually match nothing. Use .+ unless it's your intent to match nothing.

Have all your rules been listed here? Are there any errors in your error_log regarding this?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文