1

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.

3
  • $_GET['url] only exists if the file you're trying to access doesn't (that's when you RewriteRule gets triggered). If the folder /scholarship_system/ exists and there's a index.php in that folder it will use it straight away without the rewrite. Commented Nov 7, 2015 at 13:34
  • That's right, but if I remove line RewriteCond %{REQUEST_FILENAME} !-f I get this in $_GET Array ( [url] => index.php ) Commented Nov 7, 2015 at 13:45
  • That will give you more issues with your css, images and other assets. I've added an answer that should solve your problem. Commented Nov 7, 2015 at 13:52

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Ok. When I write this localhost/scholarship_system or localhost/scholarship_system/index everything is ok. But when I write this localhost/scholarship_system/index or localhost/scholarship_system/hello/hey the file css is not attached.
You're currently loading your css-file relative from URL: href="resources/style.css", Change it to absolute: href="/resources/style.css"(prepending the path with a /)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.