gpt4 book ai didi

带有查询参数的 PHP 重定向

转载 作者:行者123 更新时间:2023-12-02 17:12:04 33 4
gpt4 key购买 nike

假设我有以下 URL:

http://example.com/index.php?user/1234

我希望 PHP 查询执行的操作是将用户重定向到以下 URL:

http://example.com/test/index.php?user/1234

当然,URL 不仅要重定向?user/1234,还要重定向?anything/343。我想保持 url 不变,只向其中添加 /test/,之后的部分保持不变。

我如何做到这一点?我所能找到的只是一般重定向,而不是特定于 URL 的。谢谢。

最佳答案

如果我正确理解您的问题,您需要解析您的 URL 字符串并将“test”添加到路径中。下面的代码应该做到这一点:

// $fullUrl = $_SERVER['REQUEST_SCHEME']."://".$_SERVER[HTTP_HOST].$_SERVER[REQUEST_URI];
$fullUrl = "http://example.com/index.php?user/1234";
// split the url
$url = parse_url($fullUrl);
$url['path'] = "/test" . $url['path'];
// create the new url with test in the path
$newUrl = $url['scheme'] . "://".$url['host'].$url['path']."?".$url['query'];
header("Location:" .$newUrl);

关于带有查询参数的 PHP 重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48642495/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com