.htaccess不适用于CSS文件

时间:2015-11-07 13:26:55

标签: php css apache .htaccess mod-rewrite

我有一个.htaccess文件,其中包含以下规则:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]

我还有一个Router.php文件:

<?php

class Router
{
    function __construct()
    {
        print_r($_GET);
        $this->request = $_GET['url'];
        $this->request = rtrim($this->request, "/");
        $this->params = explode("/", $this->request);
        print_r($this->params);
        $this->controller = $this->params[0];
        if ($this->controller == "index.php")
            $this->controller = "Index";
        $this->controller = ucfirst($this->controller);
        $file = 'controllers/' . $this->controller . '.php';

        if (file_exists($file)) {
            require_once $file;
            $this->connection = new $this->controller($this->params);
        } else {
            $file = 'controllers/PageNotFound.php';
            $this->controller = "PageNotFound";
            require_once $file;
            $this->connection = new $this->controller();
        }
    }
}

header.php

<!DOCTYPE html>
<html lang="pl">
<head>
    <meta charset="utf-8">
    <link href="resources/style.css" rel="stylesheet" type="text/css">
    <title>System stypendialny</title>
</head>
<body>

我的.htaccess文件存在问题。当我使用此版本的文件并在浏览器中尝试此http://localhost/scholarship_system/网址时,我会看到:

  

数组()       注意:未定义的索引:第8行的C:\ xampp \ htdocs \ scholarship_system \ libs \ Router.php中的url       数组([0] =&gt;)

但是当我删除这一行(RewriteCond %{REQUEST_FILENAME} !-f)时,没有加载CSS文件。

1 个答案:

答案 0 :(得分:0)

你可以保持.htaccess不变。如果删除-f condition,那么路由器将需要处理对css,图像和javascript文件的所有请求,这只是一种痛苦。

在Router-class中设置默认控制器:

$this->request = isset($_GET['url'])? $_GET['url] : 'default';

然后您只需要创建文件controllers/default.php,如果未设置$_GET['url],将会使用该文件。