我正在用PHP构建基本的URL路由,我读了许多有关它的文章,但是当我尝试做同样的事情时,写方法就不会发生。这是我的代码
。htaccess
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php [QSA,L]
index.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;
}
和目录结构Directory Structure
现在,当我尝试打开/ about或/ contact时,它将把我重定向到localhost / dashboard
有人可以帮我解决这个问题吗?
尝试此操作(如果以后要使用images / js / css / ..也很有帮助)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\.(gif|jpg|png|jpeg|css|js|swf)$ /$1.$2 [END,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ index.php?$1 [L,QSA]
提示:您可以合并大小写'/'和'
<?php
$request = $_SERVER['REQUEST_URI'];
echo $request;
switch ($request) {
case '/' :
case '' : // you can combine the cases '/' and ''
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;
}
将您的.htaccess规则更改为
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
打开位于以下位置的httpd-vhosts.conf文件:C:\XAMPP\apache\conf\extra\httpd-vhosts.conf
在目录标记下添加以下行:
AllowOverride All