首页 > 解决方案 > PHP 站点在 localhost:8000 上运行正确,但在 XAMPP 或其他服务器上出现错误。如何创建.htaccess?

问题描述

这是我第一次用 PHP 创建网站。我的项目在 php interne 服务器上运行正确,但在服务器 ovh 上的 Xampp ni 上运行不正确

ovh serverur erreur: App\Router::run(): 未能打开所需的 '/home/myla/www/views/.php' (include_path='.:/usr/share/php': var $view false

localhost:8000 工作正确:var $view post/index

XAMPP erreur:尝试访问 bool 类型值的数组偏移量:var $view false

问题出在函数 run() 的 var $view 中。

public function run ():self
{
$match = $this->router->match();
$view= $match['target'] ;
$params= $match['params'];
$router= $this;
$isAdmin= strpos($view, 'admin/') !== false;
$isUser =strpos($view, 'user/') !== false;
if(!$isAdmin && !$isUser){
$layout = 'layouts/default';
}
if($isUser){
$layout = 'user/layouts/default';
}
if ($isAdmin) {
$layout = 'admin/layouts/default';
}
try{
ob_start();
require $this->viewPath . DIRECTORY_SEPARATOR . $view . '.php';
$content = ob_get_clean();
require $this->viewPath . DIRECTORY_SEPARATOR . $layout . '.php';
} catch (ForbiddenException $e) {
header('Location: ' . $this->url('login') . '?forbidden=1');
die();
}
return $this;

}

我在 index.php 中使用 run() :

$router = new App\Router(dirname(__DIR__) . '/views');
$router
->get('/', 'post/index', 'home')
->get('/blog/category/[*:slug]-[i:id]', 'category/show', 'category')
->get('/blog/[*:slug]-[i:id]', 'post/show', 'post')
->match('/login','auth/login','login')
->match('/register','auth/register','register')
->post('/logout','auth/logout','logout')
->run();

我尝试创建 .htaccess 来重写规则,但我没有找到好的配置。文件 index.php 位于 public 文件夹中,文件夹 public,src,views 位于项目的 racine 中。

RewriteEngine On

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

它不起作用。

你能帮我配置.htaccess吗?谢谢。

标签: php.htaccess

解决方案


这是一个很好的配置:

RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} /public/([^\s?]*) [NC]
RewriteRule ^ %1 [L,NE,R=302]
RewriteRule ^((?!public/).*)$ public/$1 [L,NC]

推荐阅读