【问题标题】:Error 404 page not found codeigniter 4 after upload to server上传到服务器后找不到错误 404 页面 codeigniter 4
【发布时间】:2021-10-22 17:36:14
【问题描述】:

我的项目上传到服务器后出现问题,

404 页面未找到

但是如果我使用 url mydomain.com/public 默认控制器调用并且页面欢迎正在显示,但我不能调用其他控制器。我尝试在 google 上搜索,但仍然无法正常工作,控制器和控制器类中的所有文件名都已使用大写,我还在文件夹 public_html 中添加了 .htaccess 文件。

  1. 这是我的.htaccess 文件

RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php/$1 [L]

  1. 这是我在public_html中的结构文件夹

    • 应用程序
    • 公开
    • 系统
    • 可写
    • .env
    • .gitignore
    • .htaccess
    • composer.json
    • phpunit.xml.dist
    • README.md
    • 火花

谢谢

【问题讨论】:

  • 您的索引页在哪里?它应该在 public_html 文件夹中,但是根据您的问题猜测,您可能会将它放在项目内的公用文件夹中,以便可以找到它。 404 表示未找到。检查一下。
  • 我在 public 文件夹中的 index.php

标签: php codeigniter codeigniter-4


【解决方案1】:

将域定向到公用文件夹。它应该在不输入“mydomain.com/public”的情况下加载安装。只需输入“mydomain.com”。另外,请确保您的可写文件夹及其所有子文件夹都是可写的。

【讨论】:

    【解决方案2】:

    我认为您的项目中有一个公用文件夹并且服务器根目录也命名为public_html,因此存在混淆。所以,我会在根目录下创建一个索引页面,即 public_html,只是为了将请愿书重定向到真正的索引页面所在的位置,在你的情况下,是你项目中的公共目录。然后你只需要调用 mydomain.com。我一直都是这样做的,而且效果很好。

    在 public_html 文件夹中创建一个新的索引文件并放置一个类似这样的 javascript <script type="text/javascript">setTimeout(function () { window.location.href= 'public/index.php'; }, 10);</script>

    10 是调用和真正重定向之间的时间量(以毫秒为单位)。 这会将请愿书重定向到公共文件夹中的 index.php。其余的 html 代码可能是空的,您可能希望更改背景颜色以匹配您的实际项目,这样两个索引页面之间就不会出现闪烁或粗暴的变化。

    作为参考,您可以访问我自己的网站,其中欢迎您的索引页面完全脱离了我用来构建它的离子代码。 https://www.simbiosis-dg-apps.com。不同之处在于我的重定向是通过用户点击完成的,这会将他/她带到英语或西班牙语版本。但都是一样的。

    让我为你澄清一下。会说在php 中什么都不知道,所以你必须添加那部分。但在纯 html 中,它看起来像这样:

    <!DOCTYPE html>
        <html lang="en">
        <head>
            <meta charset="UTF-8"/>
            <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
            <meta name="viewport" content="width=device-width, target-densitydpi=160dpi, initial-scale=1.0, maximum-scale=1, user-scalable=no, minimal-ui">
            <title>Your title</title>
            <meta name="description" content="your-description"/>
            <link href="your-css-folder/style.css" rel="stylesheet">
            </head>
            <body style="background:#your-background-color">
        <script>
        setTimeout(function () {
        window.location.href= 'public/index.php';
        }, 10);
        </script>
        </body>
        </html>

    【讨论】:

    • 我已将 index.php 文件从 public 移动到 public_html,但出现错误 此页面无法正常工作 mydomain.com 目前无法处理此请求。 HTTP 错误 500 –
    • 我没有从默认的 codeigniter 4 更改 index.php
    • 问题在于,如果您移动索引文件,那么您会破坏该页面所指向的内部链接。
    • 嗨@simbiosis thx 为您的回答,但我的网址现在看起来像mydomain.com/public/index.phpmydomain.com/public/。也无法调用其他控制器,出现404的页面未找到。
    • 如果你按照我说的去做,你的域名将是mydomain.com,因为 index.php 将默认包含在任何主要请求中。
    【解决方案3】:

    要解决上述问题,您应该按照以下步骤操作:

    1. public/ 目录复制index.php.htaccess 文件。

    2. 将文件添加到项目根目录。

    3. 将您的根项目目录 index.php 文件 $pathsPath = realpath(FCPATH . '../app/Config/Paths.php'); 更改为 $pathsPath = realpath(FCPATH . 'app/Config/Paths.php');

    这里是 index.php 文件的完整代码:

    // Valid PHP Version?
    $minPHPVersion = '7.2';
    if (phpversion() < $minPHPVersion)
    {
        die("Your PHP version must be {$minPHPVersion} or higher to run CodeIgniter. Current version: " . phpversion());
    }
    unset($minPHPVersion);
    
    // Path to the front controller (this file)
    define('FCPATH', __DIR__ . DIRECTORY_SEPARATOR);
    
    // Location of the Paths config file.
    // This is the line that might need to be changed, depending on your folder structure.
    $pathsPath = realpath(FCPATH . 'app/Config/Paths.php');
    // ^^^ Change this if you move your application folder
    
    /*
     *---------------------------------------------------------------
     * BOOTSTRAP THE APPLICATION
     *---------------------------------------------------------------
     * This process sets up the path constants, loads and registers
     * our autoloader, along with Composer's, loads our constants
     * and fires up an environment-specific bootstrapping.
     */
    
    // Ensure the current directory is pointing to the front controller's directory
    chdir(__DIR__);
    
    // Load our paths config file
    require $pathsPath;
    $paths = new Config\Paths();
    
    // Location of the framework bootstrap file.
    $app = require rtrim($paths->systemDirectory, '/ ') . '/bootstrap.php';
    
    /*
     *---------------------------------------------------------------
     * LAUNCH THE APPLICATION
     *---------------------------------------------------------------
     * Now that everything is setup, it's time to actually fire
     * up the engines and make this app do its thang.
     */
    $app->run();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-07-15
      • 2016-12-06
      • 2015-10-07
      • 1970-01-01
      • 2015-03-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多