1

在我的index.php(在我的项目根目录中)我有一个基本的路由系统,它获取 url(使用$_SERVER[‘REQUEST_URI])并调用正确的“控制器”,然后调用正确的“视图”(因此,实现非常基本的 MVC)。我的index.php样子是这样的:

$router= Router::load('Routes.php');
$uri=trim($_SERVER['REQUEST_URI'],'/'); 
require $router->direct($uri);

问题是,我似乎无法让所有请求都转到index.php. 显然,如果您去,localhost/myproject/您将到达index.php(因此将调用适当的控制器),但是我想要的是,即使您localhost/myproject/contact首先键入直接到index.php. 这样index.php就可以为您处理路由。

4

1 回答 1

1

如果您使用的是 apache,则可以使用.htaccessfile 重定向文件中的每个请求index.php

RewriteEngine On

RewriteRule ^/index\.php$ - [L,NC]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]

如果您使用的是 nginx 服务器,则可以将所有请求重定向到index.php文件中,并在文件中使用以下代码.conf

location / {
    try_files $uri $uri/ /index.php;
}

希望这可以帮助你

于 2020-01-28T07:48:33.737 回答