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

cod*_*nja 2 php apache .htaccess redirect

我需要重定向到index.php请求的任何目录.

所以我需要:

http://www.site.com/folder/files/hello.php
Run Code Online (Sandbox Code Playgroud)

重定向到:

http://www.site.com/folder/files/index.php
Run Code Online (Sandbox Code Playgroud)

对于任何子文件夹也是如此:

http://www.site.com/folder/files/pages/other/hello.php
Run Code Online (Sandbox Code Playgroud)

重定向到:

http://www.site.com/folder/files/pages/other/index.php
Run Code Online (Sandbox Code Playgroud)

Phi*_*hil 7

(技术上)正确的方式

构造一个URI,该URI由当前方案(http/ httpsetc),主机名和当前文件的路径组成,并发出Location头.

$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;
Run Code Online (Sandbox Code Playgroud)

这是因为位置标头URI应该是完整且绝对的.从技术上讲,不允许使用相对URI,但是有一个草案规范设置可以更改它.

务实的方式

只需发出一个相对的Location头,因为它最有可能工作.

header('Location: index.php');
exit;
Run Code Online (Sandbox Code Playgroud)