0

Here is my basic PHP router.php code:

run();

function run()
{
    $uri = explode('?', $_SERVER['REQUEST_URI'], 2);
    $routes = [
        '/' => function () {
            include(VIEWS_PATH . DS . 'accueille' . DS . 'accueille.php');
        },
        '/auth' => function () {
            include(VIEWS_PATH . DS . 'auth' . DS . 'login.php');
        },
        '/404' => function () {
            include(VIEWS_PATH . DS . '404' . DS . '404.html');
        },
    ];
    $path = $uri[0];
    echo "<pre>$path</pre>";
    switch ($path)
    {
        case '/auth':
        case '/':
            $routes[$path]();
            break ;
        default :
            $notFoundCallback = $routes['/404'];
            $notFoundCallback();
    }
}

The problem with this code is when I use "/" as a router the scripts behave as expected but when I use any other router such as "/auth" or "/404" or any other random URI that supposed to redirects to 404 page, I get the Apache 404 page not found.

Here is the index.php code

<?php declare(strict_types=1);

require_once '../config/ini.conf.php';

include(VIEWS_PATH . DS . 'globals' . DS . 'head.php');
include(CONFIG_PATH . DS . 'router.php');

My PHP program is structured as follows:

|_ app
    |_ modules
    |_ views
        |_ 404
            |_ 404.html
        |_ accuellie
            |_ accuellie.php
        |_ auth
            |_ login.php
        |_ globals
    |_ controllers
|_ config
    |_ ini.conf.php
    |_ router.php
|_ public
    |_ index.php

I have skipped irrelevant files so the question is not too long here. I need to clarify that the constants of paths are defined correctly, and accuellie.php working proves that.

I have tried some Apache configs but I still get the same errors. I also tried to grant permissions for the files but nothing changed.

I have tried some Apache configs but I still get the same errors. I also tried to grant permissions for the files but nothing changed.

site.conf file I have /var/www/site/public as DocumentRoot, and . htaccess file contains this config: RewriteEngine On

Soufiane
  • 81
  • 9
  • How is `router.php` set up to handle requests? Assuming you're using mod_rewrite, what is your configuration? – Phil Mar 20 '23 at 04:58
  • @Phil if you are talking about ``site.conf`` file I have ``/var/www/site/public`` as ``DocumentRoot`` – Soufiane Mar 20 '23 at 05:05
  • No, not really. How are requests directed to `router.php`? If you don't know, that seems to be a missing part of your configuration. I would assume you'd have some [RewriteRule](https://httpd.apache.org/docs/2.4/mod/mod_rewrite.html#rewriterule) declarations in your Apache config (or `.htaccess`). Do you? If so, what do they look like? – Phil Mar 20 '23 at 05:09
  • On .htaccess file I have RewriteEngine On – Soufiane Mar 20 '23 at 05:16
  • 1
    By default apache will only route to specific PHP files, or index.php for paths that match directories. You need to do URL rewriting if you need somehting more than that, and `RewriteEngine On` alone only turns on the feature, it doesn't configure it. There's lots of tutorials out there that can help you get your configuration right. – Evert Mar 20 '23 at 05:24

0 Answers0