CodeIgniter 4... documentRoot 未公开... htaccess 不起作用

2024-02-06

我开始涉足 CodeIgniter 4。 完成了一个简单的应用程序。在本地,我将文档根设置为 /public/ ,而在产品托管环境中,我无法将文档根设置为 /public/ 。

相反,它位于 /root 中。

所以结构是这样的:

/root
../admin
../app
../system
../public
../.htaccess
../index.php

我已将 htaccess 从公共文件夹移至根级别。

# Disable directory browsing
Options All -Indexes

# ----------------------------------------------------------------------
# Rewrite engine
# ----------------------------------------------------------------------

# Turning on the rewrite engine is necessary for the following rules and features.
# FollowSymLinks must be enabled for this to work.
<IfModule mod_rewrite.c>
    Options +FollowSymlinks
    RewriteEngine On

    # If you installed CodeIgniter in a subfolder, you will need to
    # change the following line to match the subfolder you need.
    # http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewritebase
    RewriteBase /public

    # Redirect Trailing Slashes...
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)/$ /public/$1 [L,R=301]

    # Rewrite "www.example.com -> example.com"
    RewriteCond %{HTTPS} !=on
    RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
    RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]

    # Checks to see if the user is attempting to access a valid file,
    # such as an image or css document, if this isn't true it sends the
    # request to the front controller, index.php
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1 [L]

    # Ensure Authorization header is passed along
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>

<IfModule !mod_rewrite.c>
    # If we don't have mod_rewrite installed, all 404's
    # can be sent to index.php, and everything works as normal.
    ErrorDocument 404 index.php
</IfModule>

# Disable server signature start
    ServerSignature Off
# Disable server signature end

以及根文件中的index.php:

<?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 = 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();

此外,应用程序配置文件:

<?php namespace Config;

use CodeIgniter\Config\BaseConfig;

class App extends BaseConfig
{

    /*
    |--------------------------------------------------------------------------
    | Base Site URL
    |--------------------------------------------------------------------------
    |
    | URL to your CodeIgniter root. Typically this will be your base URL,
    | WITH a trailing slash:
    |
    |   http://example.com/
    |
    | If this is not set then CodeIgniter will try guess the protocol, domain
    | and path to your installation. However, you should always configure this
    | explicitly and never rely on auto-guessing, especially in production
    | environments.
    |
    */
    public $baseURL = 'http://sub.domain.com';

    /*
    |--------------------------------------------------------------------------
    | Index File
    |--------------------------------------------------------------------------
    |
    | Typically this will be your index.php file, unless you've renamed it to
    | something else. If you are using mod_rewrite to remove the page set this
    | variable so that it is blank.
    |
    */
    public $indexPage = 'index.php';

    /*
    |--------------------------------------------------------------------------
    | URI PROTOCOL
    |--------------------------------------------------------------------------
    |
    | This item determines which getServer global should be used to retrieve the
    | URI string.  The default setting of 'REQUEST_URI' works for most servers.
    | If your links do not seem to work, try one of the other delicious flavors:
    |
    | 'REQUEST_URI'    Uses $_SERVER['REQUEST_URI']
    | 'QUERY_STRING'   Uses $_SERVER['QUERY_STRING']
    | 'PATH_INFO'      Uses $_SERVER['PATH_INFO']
    |
    | WARNING: If you set this to 'PATH_INFO', URIs will always be URL-decoded!
    */
    public $uriProtocol = 'REQUEST_URI';

    /*
    |--------------------------------------------------------------------------
    | Default Locale
    |--------------------------------------------------------------------------
    |
    | The Locale roughly represents the language and location that your visitor
    | is viewing the site from. It affects the language strings and other
    | strings (like currency markers, numbers, etc), that your program
    | should run under for this request.
    |
    */
    public $defaultLocale = 'en';

    /*
    |--------------------------------------------------------------------------
    | Negotiate Locale
    |--------------------------------------------------------------------------
    |
    | If true, the current Request object will automatically determine the
    | language to use based on the value of the Accept-Language header.
    |
    | If false, no automatic detection will be performed.
    |
    */
    public $negotiateLocale = false;

    /*
    |--------------------------------------------------------------------------
    | Supported Locales
    |--------------------------------------------------------------------------
    |
    | If $negotiateLocale is true, this array lists the locales supported
    | by the application in descending order of priority. If no match is
    | found, the first locale will be used.
    |
    */
    public $supportedLocales = ['en'];

    /*
    |--------------------------------------------------------------------------
    | Application Timezone
    |--------------------------------------------------------------------------
    |
    | The default timezone that will be used in your application to display
    | dates with the date helper, and can be retrieved through app_timezone()
    |
    */
    public $appTimezone = 'America/New_York';

    /*
    |--------------------------------------------------------------------------
    | Default Character Set
    |--------------------------------------------------------------------------
    |
    | This determines which character set is used by default in various methods
    | that require a character set to be provided.
    |
    | See http://php.net/htmlspecialchars for a list of supported charsets.
    |
    */
    public $charset = 'UTF-8';

    /*
    |--------------------------------------------------------------------------
    | URI PROTOCOL
    |--------------------------------------------------------------------------
    |
    | If true, this will force every request made to this application to be
    | made via a secure connection (HTTPS). If the incoming request is not
    | secure, the user will be redirected to a secure version of the page
    | and the HTTP Strict Transport Security header will be set.
    */
    public $forceGlobalSecureRequests = false;

    /*
    |--------------------------------------------------------------------------
    | Session Variables
    |--------------------------------------------------------------------------
    |
    | 'sessionDriver'
    |
    |   The storage driver to use: files, database, redis, memcached
    |       - CodeIgniter\Session\Handlers\FileHandler
    |       - CodeIgniter\Session\Handlers\DatabaseHandler
    |       - CodeIgniter\Session\Handlers\MemcachedHandler
    |       - CodeIgniter\Session\Handlers\RedisHandler
    |
    | 'sessionCookieName'
    |
    |   The session cookie name, must contain only [0-9a-z_-] characters
    |
    | 'sessionExpiration'
    |
    |   The number of SECONDS you want the session to last.
    |   Setting to 0 (zero) means expire when the browser is closed.
    |
    | 'sessionSavePath'
    |
    |   The location to save sessions to, driver dependent.
    |
    |   For the 'files' driver, it's a path to a writable directory.
    |   WARNING: Only absolute paths are supported!
    |
    |   For the 'database' driver, it's a table name.
    |   Please read up the manual for the format with other session drivers.
    |
    |   IMPORTANT: You are REQUIRED to set a valid save path!
    |
    | 'sessionMatchIP'
    |
    |   Whether to match the user's IP address when reading the session data.
    |
    |   WARNING: If you're using the database driver, don't forget to update
    |            your session table's PRIMARY KEY when changing this setting.
    |
    | 'sessionTimeToUpdate'
    |
    |   How many seconds between CI regenerating the session ID.
    |
    | 'sessionRegenerateDestroy'
    |
    |   Whether to destroy session data associated with the old session ID
    |   when auto-regenerating the session ID. When set to FALSE, the data
    |   will be later deleted by the garbage collector.
    |
    | Other session cookie settings are shared with the rest of the application,
    | except for 'cookie_prefix' and 'cookie_httponly', which are ignored here.
    |
    */
    public $sessionDriver            = 'CodeIgniter\Session\Handlers\FileHandler';
    public $sessionCookieName        = 'ci_session';
    public $sessionExpiration        = 7200;
    public $sessionSavePath          = WRITEPATH . 'session';
    public $sessionMatchIP           = false;
    public $sessionTimeToUpdate      = 300;
    public $sessionRegenerateDestroy = false;

    /*
    |--------------------------------------------------------------------------
    | Cookie Related Variables
    |--------------------------------------------------------------------------
    |
    | 'cookiePrefix'   = Set a cookie name prefix if you need to avoid collisions
    | 'cookieDomain'   = Set to .your-domain.com for site-wide cookies
    | 'cookiePath'     = Typically will be a forward slash
    | 'cookieSecure'   = Cookie will only be set if a secure HTTPS connection exists.
    | 'cookieHTTPOnly' = Cookie will only be accessible via HTTP(S) (no javascript)
    |
    | Note: These settings (with the exception of 'cookie_prefix' and
    |       'cookie_httponly') will also affect sessions.
    |
    */
    public $cookiePrefix   = '';
    public $cookieDomain   = '';
    public $cookiePath     = '/';
    public $cookieSecure   = false;
    public $cookieHTTPOnly = false;

    /*
    |--------------------------------------------------------------------------
    | Reverse Proxy IPs
    |--------------------------------------------------------------------------
    |
    | If your server is behind a reverse proxy, you must whitelist the proxy
    | IP addresses from which CodeIgniter should trust headers such as
    | HTTP_X_FORWARDED_FOR and HTTP_CLIENT_IP in order to properly identify
    | the visitor's IP address.
    |
    | You can use both an array or a comma-separated list of proxy addresses,
    | as well as specifying whole subnets. Here are a few examples:
    |
    | Comma-separated:  '10.0.1.200,192.168.5.0/24'
    | Array:        array('10.0.1.200', '192.168.5.0/24')
    */
    public $proxyIPs = '';

    /*
    |--------------------------------------------------------------------------
    | Cross Site Request Forgery
    |--------------------------------------------------------------------------
    | Enables a CSRF cookie token to be set. When set to TRUE, token will be
    | checked on a submitted form. If you are accepting user data, it is strongly
    | recommended CSRF protection be enabled.
    |
    | CSRFTokenName   = The token name
    | CSRFCookieName  = The cookie name
    | CSRFExpire      = The number in seconds the token should expire.
    | CSRFRegenerate  = Regenerate token on every submission
    | CSRFRedirect    = Redirect to previous page with error on failure
    */
    public $CSRFTokenName  = 'csrf_test_name';
    public $CSRFCookieName = 'csrf_cookie_name';
    public $CSRFExpire     = 7200;
    public $CSRFRegenerate = true;
    public $CSRFRedirect   = true;

    /*
    |--------------------------------------------------------------------------
    | Content Security Policy
    |--------------------------------------------------------------------------
    | Enables the Response's Content Secure Policy to restrict the sources that
    | can be used for images, scripts, CSS files, audio, video, etc. If enabled,
    | the Response object will populate default values for the policy from the
    | ContentSecurityPolicy.php file. Controllers can always add to those
    | restrictions at run time.
    |
    | For a better understanding of CSP, see these documents:
    |   - http://www.html5rocks.com/en/tutorials/security/content-security-policy/
    |   - http://www.w3.org/TR/CSP/
    */
    public $CSPEnabled = false;

}

当去http://sub.domain.com http://sub.domain.com,我正在获取默认控制器和视图渲染。 然而,链接的 CSS 资源和 JS 资源:https://sub.domain.com/assets/css/style.bundle.css https://sub.domain.com/assets/css/style.bundle.css

都会出现服务器错误。 这些文件的位置实际上是在https://sub.domain.com/public/assets/css/style.bundle.css https://sub.domain.com/public/assets/css/style.bundle.css

但我试图避免在网址中使用 /public/ 。

任何帮助或指导表示赞赏。

RD


理想情况下,您的所有文件夹都应该与 root 处于同一级别。这是假设 /root 是可公开访问的目录。

换句话说,

/admin
/app
/root
   .htaccess
   favicon.ico
   index.php
   robots.txt
   /assets
/system
/vendor
/writable

请注意,没有名为“public”的目录。实际上它以前被称为public,但已更名为“root”。没有要求必须命名public,只是它是您的域名指向的目录。

除了在 app\config 中设置一些内容之外,您应该能够使用所有已分发的文件

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

CodeIgniter 4... documentRoot 未公开... htaccess 不起作用 的相关文章

  • Zend Framework 生成唯一的字符串

    我想生成一个唯一的 4 6 个字符长的字母数字字符串 以便与每个记录 用户 一起保存在数据库中 db 字段具有唯一索引 因此尝试保存预先存在的字符串会生成错误 现在我正在生成一个随机字符串并使用 try catch 因此在添加新记录时如果抛
  • PHP 如何判断用户是否按下了 Enter 键或 Submit 按钮?

    我遇到的问题是我在一个表单中有多个提交输入 每个提交输入都有不同的值 我更愿意将它们保留为提交 Whenever the user presses Enter it is as though the topmost submit input
  • 简单的 PHP 条件帮助: if($Var1 = in list($List) and $Cond2) - 这可能吗?

    这是一个可能的功能吗 我需要检查一个变量是否存在于我需要检查的变量列表中 并且 cond2 是否为 true 例如 if row name 1 2 3 Cond2 doThis 它对我不起作用 我在复制粘贴中更改的只是我的列表和变量名称 i
  • PHP MySQL 使用选项/选择 HTML 表单标签进行多重搜索查询

    我正在尝试使用两个搜索字段设置基本的 MySQL LIKE 搜索 我不想拥有它 所以它有多个可选搜索字段 例如if isset POST city isset POST name 我不知道如何用 HTML 来做到这一点
  • 检查 PHP 中“@”字符后面的单词

    我现在正在制作一个新闻和评论系统 但是我已经在一个部分上停留了一段时间了 我希望用户能够在 Twitter 上引用其他玩家的风格 例如 用户名 该脚本看起来像这样 不是真正的 PHP 只是想象脚本 3 string I loved the
  • 如何从父类函数访问子类中定义的常量?

    我从 php net 看到这个例子 但 c MY CONST 仅在 5 3
  • 如何使用 php 下载/打印页面的特定部分

    我有一个 HTML 页面如下 Lorem Ipsum is simply dummy text of the printing and typesetting industry Lorem Ipsum has been the indust
  • 将“php”作为 shell 脚本执行时的自定义 php.ini 文件

    我在跑php作为 shell 脚本 我不确定 shell脚本 是否正确 该文件以 usr bin php 这很好用 但 MongoDB 类没有正确加载php ini文件 具有extension mongo so 未使用 我该如何使用它tha
  • 是否可以使用 PHP 重定向发送 POST 数据?

    更新 这不是重复的如何使用 PHP 发送 POST 请求 https stackoverflow com questions 5647461 how do i send a post request with php 那里的解决方案对我不起
  • preg_match_all 查询仅显示有问题的外部组

    我无法弄清楚如何只显示 preg 查询的外部组级别 我会给你一个例子 preg match all start end input matches 这个输入start1 start2 2end 1end产生这个输出start1 start2
  • “使用未定义常量”注意,但该常量应该被定义

    共有三个文件 common php controller php 和 user php 文件 common php 如下所示 文件controller php看起来像 文件 user php 如下所示 执行脚本时 会给出通知 注意 使用未定
  • 使用 SSL 证书验证 Web 浏览器

    是否可以使用 ssl 证书对 Web 浏览器进行身份验证 假设我在应用程序中存储私钥 有什么方法可以从浏览器读取密钥并尝试基于该私钥进行身份验证 您可以使用 SSL TLS 客户端证书身份验证来对浏览器 用户进行身份验证 服务器必须请求客户
  • PHP 在输入流中使用 fwrite 和 fread

    我正在寻找将 PHP 输入流的内容写入磁盘的最有效方法 而不使用授予 PHP 脚本的大量内存 例如 如果可以上传的最大文件大小为 1 GB 但 PHP 只有 32 MB 内存 define MAX FILE LEN 1073741824 1
  • PHP 脚本可以在终端中运行,但不能在浏览器中运行

    我正在尝试执行exec命令 但我遇到了问题 当我运行以下代码时 当我通过浏览器运行它时它不起作用 但如果我把输出 str将其复制并粘贴到终端中 它工作得很好 造成这种情况的原因是什么 我该如何解决 目前我正在运行localhost php
  • PHP 与 MySQL 查询性能( if 、 函数 )

    我只看到这个artice http www onextrapixel com 2010 06 23 mysql has functions part 5 php vs mysql performance 我需要知道在这种情况下什么是最好的表
  • 在 apache docker 容器中运行虚拟主机

    我在同一个 apache 容器中有两个 php 应用程序 我试图在端口上运行其中一个应用程序 因为它需要通过根域而不是子文件夹进行访问 我想在端口 8060 上运行应用程序 我尝试使用 apache 虚拟主机执行此操作 但它不会加载页面 h
  • SQL 最近日期

    我需要在 php 中获取诸如 2010 04 27 之类的日期作为字符串 并在表中找到最近的 5 个日期 表中的日期保存为日期类型 您可以使用DATEDIFF http dev mysql com doc refman 5 1 en dat
  • 表单计算器脚本基本价格未加载 OnLoad

    我的表单中有一个计算器来计算我的下拉选项选择 function select calculate on change calc input type checkbox calculate on click calc function cal
  • 一次播种多行 laravel 5

    我目前正在尝试为我的用户表播种 如果我像这样尝试 2 行 就会失败 如果我只使用单个数组而不是 users 数组内的 2 个数组来创建一些假数据 那么效果很好 我做错了什么 正确的方法是什么 class UserTableSeeder ex
  • 如何在 Laravel 中使用 PUT http 动词提交表单

    我知道这个问题可能已经提出 但我就是无法让它发挥作用 如果有人可以帮助我 我将非常感激 我安装了 colletive form 但答案也可以是 html 表单标签 现在列出我的表格 我的路线和我的例外情况 Form model array

随机推荐