PHP 中的基本 URL 路由使用 .htaccess 不起作用

我正在用Php构建一个基本的URL路由,我阅读了许多关于它的文章,但是当我尝试做同样的事情时,不会以写入方式发生。这是我的代码


.htaccess


RewriteEngine On

RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond %{REQUEST_FILENAME} !-f

RewriteRule ^(.+)$ index.php [QSA,L]


索引.php页


<?php


$request = $_SERVER['REQUEST_URI'];

echo $request;

switch ($request) {

    case '/' :

        require __DIR__ . '/views/home.php';

        break;

    case '' :

        require __DIR__ . '/views/home.php';

        break;

    case '/about' :

        require __DIR__ . '/views/about.php';

        break;

    default:

        http_response_code(404);

        require __DIR__ . '/views/404.php';

        break;

}

和目录结构 目录结构


现在,当我尝试打开/关于或/联系人时,它会将我重定向到本地主机/仪表板


有人可以帮我解决这个问题吗?


Helenr
浏览 118回答 3
3回答

胡子哥哥

试试这个(如果你想使用 images/js/css/.. 稍后也很有用)RewriteCond %{REQUEST_FILENAME} !-fRewriteCond %{REQUEST_FILENAME} !-dRewriteRule ^(.*)\.(gif|jpg|png|jpeg|css|js|swf)$ /$1.$2 [END,NC]RewriteCond %{REQUEST_FILENAME} !-fRewriteCond %{REQUEST_FILENAME} !-dRewriteCond %{REQUEST_FILENAME} !-lRewriteRule ^(.*)$ index.php?$1 [L,QSA]提示:您可以组合大小写“/”和”<?php$request = $_SERVER['REQUEST_URI'];echo $request;switch ($request) {&nbsp; &nbsp; case '/' :&nbsp; &nbsp; case '' : // you can combine the cases '/' and ''&nbsp; &nbsp; &nbsp; &nbsp; require __DIR__ . '/views/home.php';&nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; case '/about' :&nbsp; &nbsp; &nbsp; &nbsp; require __DIR__ . '/views/about.php';&nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; default:&nbsp; &nbsp; &nbsp; &nbsp; http_response_code(404);&nbsp; &nbsp; &nbsp; &nbsp; require __DIR__ . '/views/404.php';&nbsp; &nbsp; &nbsp; &nbsp; break;}

慕姐8265434

将 .htaccess 规则更改为RewriteEngine OnRewriteCond %{REQUEST_FILENAME} !-dRewriteCond %{REQUEST_FILENAME} !-fRewriteRule ^ index.php [L]

跃然一笑

打开位于此处的 httpd-vhosts.conf 文件:C:\XAMPP\apache\conf\extra\httpd-vhosts.conf在目录标记下添加以下行:AllowOverride All
打开App,查看更多内容
随时随地看视频慕课网APP