2015-11-07 71 views
1

我有一个.htaccess文件,这些规则的工作:的.htaccess不与CSS文件

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/ URL我看到这一点:

阵列() 公告:未定义指数:用C网址:\ XAMPP \ htdocs中\ scholarship_system \库\路由器在线路8 .PHP 阵列([0] =>)

但是,当我删除此行(RewriteCond %{REQUEST_FILENAME} !-f),则CSS文件未加载。

+0

'$ _GET ['url]'只存在于你试图访问的文件不存在的时候(这是你RewriteRule被触发的时候)。如果文件夹'/ scholarship_system /'存在,并且该文件夹中有一个'index.php',它将立即使用它而不用重写。 –

+0

这是正确的,但如果我删除行RewriteCond%{REQUEST_FILENAME}!-f 我得到这$ _GET阵列([url] => index.php) –

+0

这会给你更多的问题与您的CSS,图像和其他资产。我已经添加了一个可以解决您的问题的答案。 –

回答

0

你可以保留你的.htaccess。如果你删除了-f条件,你就需要路由器处理所有对css,图像和javascript文件的请求,这只是一个痛苦。

在你的路由器级设置一个默认的控制器,而不是:

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

,那么你只需要创建如果$_GET['url]没有设置将被使用的文件controllers/default.php

+0

非常感谢。有用。 –

+0

好的。当我写这个http:// localhost/scholarship_system或http:// localhost/scholarship_system/index时,一切正常。但是,当我写这个http:// localhost/scholarship_system/index /或http:// localhost/scholarship_system/hello/hey时,文件css没有附加。 –

+0

你目前正在从URL加载你的css文件相关:'href =“resources/style.css”',把它改为绝对:'href =“/ resources/style.css”'(用' /') –