我需要重定向到请求目录的 index.php
。
所以我需要:
http://www.site.com/folder/files/hello.php
要重定向到:
http://www.site.com/folder/files/index.php
对于任何子文件夹也是如此:
http://www.site.com/folder/files/pages/other/hello.php
重定向到:
http://www.site.com/folder/files/pages/other/index.php
最佳答案
(技术上)正确的方法
构造一个由当前方案(http
/https
/等)、主机名和当前文件路径组成的 URI,并发出一个位置 标题。
$url = sprintf('%s://%s%s/index.php',
$_SERVER['SERVER_PORT'] == 80 ? 'http' : 'https',
$_SERVER['SERVER_NAME'], rtrim(dirname($_SERVER['PHP_SELF']), '/'))
header("Location: $url");
exit;
这是因为位置 header URI 应该是完整且绝对的。相对 URI 在技术上是不允许的,但是有一个规范草案集来改变这一点。
务实的方式
只需发布一个相对的 Location header ,因为它很可能会起作用。
header('Location: index.php');
exit;
关于php - 重定向到请求子文件夹的 index.php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19440828/