I have a php project in htdocs folder named “das-xampp”. The index file in the root directory works as a router. I have another index.php inside views. so the structure is as below:
das-xamp |__index.php |__views |__index.php |__about-us.php |__404-Not_Found.php
So whenever someone types ‘localhost/das-xampp’ it should re-route the user to the index inside ‘views/index.php’ My root index(the one that works as a router) is as follows:
<?php $path = trim($_SERVER['REQUEST_URI'], '/'); parse_url($path, PHP_URL_PATH); $routes = [ ''=> 'views/index.php', 'about-us' => 'views/about-us.php' ]; if (array_key_exists($path,$routes)) { require $routes[$path]; }else { require 'views/404-Not-Found.php'; } ?>
The thing is whenever I type in ‘localhost/das-xampp'(after turning on apache and mysql), the not-found php comes up. Even when I type ‘localhost/das-xampp/about-us’ manually Object not found is shown.
This doens’t happen if I use
"php -S localhost:<some_digit>"
All my view works fine.
Advertisement
Answer
The problem was with .htaccess file. I didn’t include an .htaccess file which will re-route according to the routings I had in index.php file(the one in the root directory). I’m posting the .htaccess code below
RewriteEngine On RewriteBase / RewriteRule ^index.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /das/index.php [L]