php - 重定向到请求子文件夹的 index.php

标签 php apache .htaccess redirect

我需要重定向到请求目录的 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/

相关文章:

PHP : how to pass database value into a javascript/jquery function

html - Chrome 不会显示文件系统图像

css - 通过 .htaccess 防止通过 CSS 进行热链接

wordpress - .htaccess重写条件忽略子域

php - 使用套接字传递按钮单击事件

javascript - 为教育形式创建可解锁的内容

php - MySQL LEFT JOIN 列为空

apache - Silex 位于本地主机 : "Sorry, the page you are looking for could not be found" 上的子目录中

macos - 如何在 mac 上安装 apache 基准测试

apache - Elasticsearch :匹配词组产生不必要的结果