如果我导航到一个url,PHP-CSS就不会被阅读


PHP - CSS Not being read if I navigate to a url

我的网站使用URL分段来拥有动态页面
我基本上加载php文件的内容,而不是重定向到它们。一切都很好,导航到CSS没有加载的第二个URL段。

请允许我详细说明:mydomain.com/foo将"root/somedirectory/foo/index.php"加载到我的主index.php.中的<body>

mydomain.com/foo/bar将"root/somedirectory/foo/bar.php"加载到我的主index.php 中的<body>

问题是:当我转到第二个URL(URL中有条)时,主index.php的CSS不再加载,控制台也会收到错误信息。

这很奇怪,因为它的逻辑基本上与我不添加bar时相同(因此加载index.php)

错误:http://oi68.tinypic.com/148pzec.jpg

我的代码(main index.php)

if(!urlSegment(1)) include("home.php");
else{
    $target = strtolower(urlSegment(1));
    $targetFolder = FILE_PATH.$target."/";
    $fileName = !urlSegment(2) ? "index.php" : strtolower(urlSegment(2)).".php";
    $fileTarget = $targetFolder.$fileName;
    if(file_exists($fileTarget)) include($fileTarget);
    else{
        echo 'Page not found ('.$fileTarget.')';
    }
}

urlSegment函数只是将url的一部分作为字符串获取
如果您有"www.stackeoverflow.com/foo/bar/donut"foo=1,bar=2,donut=3

提前感谢!

编辑后完整的(主)index.php如下所示:

<html>
   <head>
      <!-- Bunch of CSS / Javascript Links -->
   </head>
   <body>
      <?php /*php The code above */ ?>
   </body>
</html>

"bar.php"基本上有普通的HTML,这些HTML将包含在<body>标记中。它使用<head>标记中CSS链接的样式。请记住,一个URL段就像一个魅力,第二个会打破它

这会在控制台中为每个.css文件弹出:Resource interpreted as Stylesheet but transferred with MIME type text/html

当我导航到一个URL段时,通过控制台看到的文件夹结构(就像它应该一样):
http://oi64.tinypic.com/2qwmpuo.jpg

当我导航到两个URL段时,通过控制台看到的文件夹结构(就像它不应该一样):
http://oi67.tinypic.com/2lo3tl4.jpg

您的代码的其余部分是个谜,但作为快速修复方法,请尝试以下操作:

$path = (explode('?', $_SERVER['REQUEST_URI']))[0];  // request path
$mime = array_pop((explode('.', $path)));  // file extension
if ($mime == 'php')
{
    // ... your code here
}
else
{
    if (!is_readable($path))
    {
        $fail = 'HTTP/1.0 404 Not Found';
        header(!file_exists($path) ? $fail : 'HTTP/1.0 403 Forbidden');
        exit;
    }
    ob_get_clean(); // clear output buffer that could cause issues
    header('HTTP/1.0 200 OK');
    // ... send appropriate headers here, according to mime & size
    readfile($path);
    exit;
}

其想法是简单地"服务"任何其他不是"php"文件的文件。


更新

上面的"修复"将有助于隔离处理不同文件类型的条件。

图片中显示的错误可能是由JavaScript输出中的PHP错误引起的,这也可能导致CSS没有按预期显示。