PHP:如何获取最后的目录路径并回显它们


PHP: How to get the last directory paths and echo them

我的.htaccess和index.php文件位于"localhost/poppers/f/"中。我有index.php处理的所有来自"/f/"目录的url请求。我想要得到的和echo只是"f"目录之后的整个路径,无论它包含多少尾部斜杠。

示例:localhost/poppers/f/abcdef/123456/xyz/456

我想要的输出是:abcdef/123456/xyz/456(与以下目录的数量无关)

我尝试使用的是:dirname(dirname($_SERVER['REQUEST_URI'])),但似乎不起作用。

您可以在重写规则中添加一个环境变量,并在PHP代码中访问它:

保持你的/poppers/f/.htaccess像这样:

RewriteEngine On
RewriteBase /poppers/f/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.+) index.php [L,E=URIPART:$1]

这将把每个请求路由到index.php,并将来自/poppers/f/的相对路径设置为env变量URIPART

现在,在您的index.php中,只需将此值获取为:

$_SERVER["REDIRECT_URIPART"]

将显示值:abcdef/123456/xyz/456

$string = "localhost/poppers/f/abcdef/123456/xyz/456";
$parts = explode('/', $string);

在这一点上,你可以简单地移除前两个部分,然后内爆()其余部分…

echo implode( array_slice($parts,3), '/' );