在 php 脚本中访问重写的 URI

发布于 2024-09-28 07:21:41 字数 359 浏览 0 评论 0原文

所以我有一个 .htaccess 文件,它正在执行从 /testscript1.php/testvar1/testvar2 的重写 /testscript2.php/testvar3/testvar4 (这是一个过于简化的例子,但你明白了)。

现在,虽然在我的 testscript2.php 脚本中,当我访问 $_SERVER['REQUEST_URI'] 变量时,我看到 /testscript1.php/testvar1/testvar2 而不是 /testscript2.php/testvar3/testvar4 ,这就是我正在寻找的。即 $_SERVER['REQUEST_URI'] 包含重写之前的 uri。

我的问题很简单,有没有办法访问重写的uri?

So I have a .htaccess file which is performing a rewrite from /testscript1.php/testvar1/testvar2 to
/testscript2.php/testvar3/testvar4
(this is an over simplification but you get the idea).

Now though in my testscript2.php script when i access the $_SERVER['REQUEST_URI'] variable i see /testscript1.php/testvar1/testvar2 rather than /testscript2.php/testvar3/testvar4 which is what I am looking for. i.e $_SERVER['REQUEST_URI'] contains the uri before the rewrite.

My question is simply, is there a way to access the rewritten uri?

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

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

发布评论

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

评论(2

兔小萌 2024-10-05 07:21:41

尝试使用 phpinfo() 来查看 $_SERVER 在重写的页面上的外观。 Apache 提供了大量可能有用的信息。

在我的测试服务器上,我得到以下内容可能会对您有所帮助:

$_SERVER["REDIRECT_QUERY_STRING"]
$_SERVER["REDIRECT_URL"]
$_SERVER["QUERY_STRING"]
$_SERVER["REQUEST_URI"]
$_SERVER["SCRIPT_NAME"]
$_SERVER["PHP_SELF"]

我希望至少其中一个或其中的组合应该能够可靠地为您提供您正在寻找的信息。

干杯。

Try using phpinfo() to get a view on what $_SERVER looks like on a rewritten page. Apache supplies quite a lot of info that may be useful.

On my test server, I get the following which may help you:

$_SERVER["REDIRECT_QUERY_STRING"]
$_SERVER["REDIRECT_URL"]
$_SERVER["QUERY_STRING"]
$_SERVER["REQUEST_URI"]
$_SERVER["SCRIPT_NAME"]
$_SERVER["PHP_SELF"]

I would expect that at least one or a combination of those should be able to reliably give you the information you're looking for.

Cheers.

左秋 2024-10-05 07:21:41

如果您使用路径信息来传递附加路径,则可以从PHP_SELF中删除该后缀:

substr(parse_url($_SERVER['PHP_SELF'], PHP_URL_PATH), -strlen($_SERVER['PATH_INFO']))

或者直接使用SCRIPT_NAME,因为 >PHP_SELF = SCRIPT_NAME + PATH_INFO。只需查看 $_SEVER 中的各种值即可。

If you’re using the path info to pass an additional path, you can strip that suffix from PHP_SELF:

substr(parse_url($_SERVER['PHP_SELF'], PHP_URL_PATH), -strlen($_SERVER['PATH_INFO']))

Or simply use SCRIPT_NAME since PHP_SELF = SCRIPT_NAME + PATH_INFO. Just take a look at the various values in $_SEVER.

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