URL Routing in PHP : url routes ony to 404-Not-Found page

↘锁芯ラ 提交于 2020-12-15 07:18:11

问题


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.

not-found page


回答1:


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]


来源:https://stackoverflow.com/questions/63214342/url-routing-in-php-url-routes-ony-to-404-not-found-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!