Apache 的 mod_rewrite 和 %{REQUEST_URI} 问题

发布于 2024-07-29 19:31:01 字数 893 浏览 2 评论 0原文

假设我们有以下 PHP 页面“index.php”:

<?
if (!isset($_GET['req'])) $_GET['req'] = "null";
echo $_SERVER['REQUEST_URI'] . "<br>" . $_GET['req'];
?>

和以下“.htaccess”文件:

RewriteRule ^2.php$ index.php?req=%{REQUEST_URI}
RewriteRule ^1.php$ 2.php

现在,让我们访问“index.php”。 我们得到这个:

/index.php
null

那很酷。 让我们访问“2.php”。 我们得到这个:

/2.php
/2.php

这也很酷。 但现在让我们看一下“1.php”:

/1.php
/2.php

所以......我们请求“1.php”,它默默地重定向到“2.php”,后者默默地重定向到“index.php?req=%{REQUEST_URI}” ”,但这里的“%{REQUEST_URI}”似乎是“2.php”(我们在第一次重定向之后寻找的页面),而 $_SERVER['REQUEST_URI'] 是“ 1.php”(原始请求)。

这些变量不应该相等吗? 今天这让我很头疼,因为我试图仅根据原始请求进行重定向。 我可以在“.htaccess”中使用任何变量,即使在重定向后也能告诉我原始请求吗?

预先感谢,我希望我已经说清楚了。 这是我在这里的第一篇文章:)

suppose we have the following PHP page "index.php":

<?
if (!isset($_GET['req'])) $_GET['req'] = "null";
echo $_SERVER['REQUEST_URI'] . "<br>" . $_GET['req'];
?>

and the following ".htaccess" file:

RewriteRule ^2.php$ index.php?req=%{REQUEST_URI}
RewriteRule ^1.php$ 2.php

Now, let's access "index.php". We get this:

/index.php
null

That's cool. Let's access "2.php". We get this:

/2.php
/2.php

That's cool too. But now let's have a look at "1.php":

/1.php
/2.php

So... we ask for "1.php", it silently redirects to "2.php" which silently redirects to "index.php?req=%{REQUEST_URI}", but here the "%{REQUEST_URI}" seems to be "2.php" (the page we're looking for after the first redirection) and the $_SERVER['REQUEST_URI'] is "1.php" (the original request).

Shouldn't these variables be equal? This gave me a lot of headaches today as I was trying to do a redirection based only on the original request. Is there any variable I can use in ".htaccess" that will tell me the original request even after a redirection?

Thanks in advance and I hope I've made myself clear. It's my first post here :)

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

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

发布评论

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

评论(3

还给你自由 2024-08-05 19:31:01

我不确定它是否能满足您的需求,但请尝试先查看 REDIRECT_REQUEST_URI,如果不存在,则再查看 REQUEST_URI。 您在对 Gumbo 的回答的评论中提到,您真正要查找的是原始 URI; Apache 尝试使用服务器变量的 REDIRECT_* 版本来实现此类功能。

I'm not sure whether it will meet your needs, but try looking at REDIRECT_REQUEST_URI first, then if it's not there, REQUEST_URI. You mention in your comment to Gumbo's answer that what you're truly looking for is the original URI; REDIRECT_* versions of server variables are how Apache tries to make that sort of thing available.

掌心的温暖 2024-08-05 19:31:01

只需更改规则的顺序即可:

RewriteRule ^1\.php$ 2.php
RewriteRule ^2\.php$ index.php?req=%{REQUEST_URI}

或者仅使用一条规则:

RewriteRule ^(1|2)\.php$ index.php?req=%{REQUEST_URI}

Just change the order of the rules and it works:

RewriteRule ^1\.php$ 2.php
RewriteRule ^2\.php$ index.php?req=%{REQUEST_URI}

Or use just one rule:

RewriteRule ^(1|2)\.php$ index.php?req=%{REQUEST_URI}
守护在此方 2024-08-05 19:31:01

好吧,我想我解决了问题。 我使用了 %{THE_REQUEST} 变量,它基本上包含这样的内容:“GET /123.php HTTP/1.1”。 即使重定向后它仍然保持不变。 感谢大家的帮助! :)

Well I guess I solved the problem. I used the %{THE_REQUEST} variable which basically contains something like this: "GET /123.php HTTP/1.1". It remains the same even after a redirection. Thanks everyone for your help! :)

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