繁体   English   中英

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

[英]How to add to the paths “index.html”

如果尚未选择文件,如何将index添加到路径? 我的意思是,如果用户进入to/this/page ,它会改变它to/this/page/index.htmlto/this/page/index.php ,如果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数组以获取更多信息

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM