Dominik Adamski
Dominik Adamski

Reputation: 164

.htaccess doesn't work with CSS files

I have a .htaccess file with these rules:

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

I also have a Router.php file:

<?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();
        }
    }
}

and 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>

I have a problem with the .htaccess file. When I use this version of the file and I try this http://localhost/scholarship_system/ URL in the browser I see this:

Array ( ) Notice: Undefined index: url in C:\xampp\htdocs\scholarship_system\libs\Router.php on line 8 Array ( [0] => )

But when I remove this line (RewriteCond %{REQUEST_FILENAME} !-f) then the CSS file is not loaded.

Upvotes: 1

Views: 1473

Answers (1)

M. Eriksson
M. Eriksson

Reputation: 13635

You can keep your .htaccess as it is. If you remove -f condition, you're router will need to handle all requests to css, images and javascript-files as well and that's just a pain.

Set a default controller in your Router-class instead:

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

then you just need to create the file controllers/default.php which will be used if the $_GET['url] isn't set.

Upvotes: 1

Related Questions