php - 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吗?谢谢。
解决方案
这是一个很好的配置:
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} /public/([^\s?]*) [NC]
RewriteRule ^ %1 [L,NE,R=302]
RewriteRule ^((?!public/).*)$ public/$1 [L,NC]
推荐阅读
- php - 插入数据库时数组到字符串的转换错误
- python - 创建一个循环的最佳方法,将矩阵乘以它的每个元素,然后对结果求和
- python - 节点红色守护进程
- javascript - 如何优化 Google Apps 脚本代码以防止超出最大执行时间?
- java - Kafka Stream 应用程序可以从 Kinesis 流中读取吗?
- aws-lambda - CloudFormation 创建的 Lambda 不创建 LogStream/Logs
- python - 获取数字“1”出现在我的列表中的次数,而不使用 if 语句或 for 循环中的空列表?
- c# - 创建一个可以有多种类型的构造函数属性?
- android - 如何在带有 Viewpager 和 Fragment 占位符的选项卡式活动中使用 OnClickListener
- python - 每次运行函数时如何增加函数变量