PHP中的URL路由:url仅将路由到404-Not-Found页面 [英] URL Routing in PHP : url routes ony to 404-Not-Found page

查看:71
本文介绍了PHP中的URL路由:url仅将路由到404-Not-Found页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在htdocs文件夹中有一个名为"das-xampp"的php项目.根目录中的索引文件充当路由器.我在视图中还有另一个index.php.所以结构如下:

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

因此,每当有人键入"localhost/das-xampp"时,都应将用户重新路由到"views/index.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';
}


?>

问题是,每当我键入"localhost/das-xampp"(打开apache和mysql后),就会出现未找到的php.即使我手动键入"localhost/das-xampp/about-us",也会显示未找到对象.

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

我所有的观点都很好.

找不到页面

推荐答案

.htaccess文件存在问题.我没有包括.htaccess文件,该文件将根据index.php文件(根目录中的那个)中的路由进行重新路由.我在下面发布了.htaccess代码

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]

这篇关于PHP中的URL路由:url仅将路由到404-Not-Found页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆