添加'/';使网站崩溃


Adding a '/' makes the website crash

这就是没有'/'(localhost/index.php)的情况:http://postimg.org/image/jxg239qzn/

在url(localhost/index.php/)末尾添加后:http://postimg.org/image/e01pe0isv/

任何有经验的人都可以简要介绍一下可能的原因。

这是index.php:

require_once("php/config.php");
if(substr_count($_SERVER['PHP_SELF'],'/')==1){ // fixing that bug  
    require_once( ROOT_PATH . "php/header.php" );   
    if(isvalid(quizzid()) == true){
        switch(quizzid()){
            case 0:
                require_once( ROOT_PATH . "php/questionarios.php" );
                break;
            default:
                require_once( ROOT_PATH . "php/questionario.php" ); 
                break;
         } 
     }          
     require_once(ROOT_PATH . "php/footer.php");    // footer
}else{ // if there are more than 1 '/' it would redirect to > index
    header('Location: /index.php');
}

屏幕截图显示,当您在URL末尾添加尾部斜杠时,您的网站不会加载外部文件(CSS、JS、图像)。

最可能的原因是您对外部文件使用了相对路径。

<link rel="stylesheet" type="text/css" href="style.css">

您的浏览器将尝试加载http://localhost/index.php/style.css而不是http://localhost/style.css


使用外部文件的绝对路径将更正此问题。

<link rel="stylesheet" type="text/css" href="/style.css">

您还可以在HTML中使用<base>元素,它将配置从特定目录中提取的所有相对路径:这将影响所有CSS/JS/图像文件,因此这是一个比将所有相对路径更改为绝对路径更快的解决方案。

<base href="http://localhost/">

我还建议在Apache的配置中将AcceptPathInfo设置为Off。这将向http://localhost/index.php/发出请求以获得404 Not Found响应。

我猜服务器假设"index.php"是一个目录,并寻找路径为"localhost/index.php/index.php"的索引文件。您测试过这是否是潜在的问题吗?

添加/后,include路径从localhost更改为localhost/index.php/,并在不存在的localhost/index.php/php/config.php位置搜索config.php。

因此,添加/后不会设置ROOT_PATH。

使用绝对路径(完整路径),同时包括配置

require_one("/php/config.php");

我刚刚意识到问题出在哪里,真为那个bug的愚蠢感到羞愧。

在我过去包含img、css、js或其他任何东西的地方,我都是这样使用的:

(之前)link rel="stylesheet"type="text/css"href="css/css.css"

(在)link rel="stylesheet"type="text/css"href="/css/css.css"

这里有一个答案很好的问题:CSS文件的相对路径

谢谢你,如果你对我的愚蠢感到恼火,我很抱歉。

(我想在每个答案上加1,但我需要15的声誉。)