如何在没有 subfolder/subdirectory 的情况下 return URL(使用 PHP 而不是 htaccess)

How to return a URL without a subfolder/subdirectory (using PHP not htaccess)

问题基本上就涵盖了。我确定有人问过这个问题,但我似乎找不到它。

澄清一下,我有这个 url

https://www.example.com/subfolder/page.php

我想 return 这个 url

https://www.example.com/page.php

我想我应该使用 http_build_url() 但唉,我在这里。

我知道这可以用 .htaccess 完成,但对于我的特殊情况,我需要在 php.

中完成

最简单的方法是,您可以重定向:

header('location: example.com/page.php');

根据需要使用 http_build_url(),您可以这样做:

echo http_build_url("http://example.com/subfolder/page.php",
array(
    "scheme" => "http",
    "host" => "example.com",
    "path" => "/page.php"
),
HTTP_URL_STRIP_AUTH | HTTP_URL_JOIN_PATH | HTTP_URL_JOIN_QUERY | HTTP_URL_STRIP_FRAGMENT
);