如何添加到路径“;index.html”;


How to add to the paths "index.html"

如果index还没有选择文件,我如何将其添加到路径中?我的意思是,如果用户输入to/this/page,它会将其更改为to/this/page/index.html,如果index.php不存在,则会将其改为to/this/page/index.php。如果index.phpindex.html都不存在,我希望它回显error

以下是我尝试过的:

        if(!file_exists($filename)) {
            if(strpos($filename, ".") === false){
                $nfilename = $filename . "index.html";
                if(!file_exists($nfilename)){
                    $filename .= "index.php";
                    if(!file_exists($filename)){
                        echo 'error';
                    }
                } else $filename = $nfilename;
            } else {
                echo 'error';
            }
        }

您可以使用以下代码检查请求uri是否有index.php或index.html:

if (strpos($_SERVER['REQUEST_URI'], 'index.php') === false
    || strpos($_SERVER['REQUEST_URI'], 'index.html') === false) {
    // redirect to $_SERVER['REQUEST_URI'] plus one of the index
}

查看$_SERVER数组以获取更多信息