【问题标题】:How to remove public/index.php/ from url in Codeigniter 4如何从 Codeigniter 4 中的 url 中删除 public/index.php/
【发布时间】:2018-06-30 18:14:27
【问题描述】:

我想要像 www.sample.com/controller 这样的普通 URL,而不是 www.sample.com/public/index.php/controller

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

我的 .htaccess 文件

【问题讨论】:

  • 谷歌.htaccess
  • 感谢您回复刚刚编辑的问题

标签: php codeigniter-4


【解决方案1】:

我不知道你是怎么得到这个.htaccess的。如果您 download CodeIgniter 4 from here 并且您可以看到 Htaccess 文件与您添加的文件不同。

由于 CodeIgniter (1,2,3) 对 Codeigniter 4 有很多更改,htaccess 移至 public/ 文件夹,这将 设置为文档根目录.htaccess在 CI 4 中将无效


.htaccess的正确路径

public/.htaccess

注意:我刚刚测试了index.php URL 问题,并没有这样的问题。没有index.php,工作/加载很好

【讨论】:

    【解决方案2】:

    /public 中创建一个 .htaccess 文件:

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

    【讨论】:

      【解决方案3】:

      第一步:在 public/ 目录下,将 index.php 和 .htaccess 复制到你的项目根目录。

      第二步:在项目根目录下,打开 index.php 并编辑以下行:

      改变:

      $pathsPath = FCPATH 。 '../app/Config/Paths.php';

      改为

      ($pathsPath = FCPATH .'app/Config/Paths.php';)

      enter image description here

      【讨论】:

        【解决方案4】:

        你试过这个吗:

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

        【讨论】:

          【解决方案5】:

          好的,这就是我解决这个问题的方法: 您需要 2 个 .htaccess 文件
          项目根路径“/.htaccess”中的第一个 公共文件夹“public/.htaccess”中的第二个

          这是第一个“/.htaccess

          # this is my version (Nassim) of the htaccess file , I use this one in the root directory "/"
          # of the project and a second .htaccess in the public directory
          
          DirectoryIndex /public/index.php
          RewriteEngine on
          RewriteCond $1 !^(index\.php|images|assets|css|js|robots\.txt|favicon\.ico)
          RewriteCond %{REQUEST_FILENAME} !-f
          RewriteCond %{REQUEST_FILENAME} !-d
          RewriteRule ^(.*)$ ./public/index.php/$1 [L,QSA]
          

          第二个(附带 CI 安装):“public/.htaccess

          # I recommend you remove `IfModule`. Because if you need mod_rewrite,
          # you don't need `IfModule`. If you don't need it, you don't need this file
          # at all.
          <IfModule mod_rewrite.c>
          RewriteEngine On
          RewriteCond $1 !^(index\.php|images|assets|doc|data|robots\.txt)
          RewriteCond %{REQUEST_FILENAME} !-f
          RewriteCond %{REQUEST_FILENAME} !-d
          RewriteRule ^(.*)$ index.php/$1 [L]
          </IfModule>
          

          【讨论】:

          • 根目录除了.htaccess文件还需要什么?
          • 抱歉 @arunkumar ,忙着回复,仅此而已
          • @HeribertoJuárez 太棒了!
          【解决方案6】:

          做吧

          第一步:在 public/ 目录下,将 index.php 和 .htaccess 复制到你的项目根目录。

          第二步:在项目根目录下,打开 index.php 并编辑以下行:

          index.php -> $pathsPath = FCPATH 。 '../app/Config/Paths.php';

          index.php => $pathsPath = FCPATH 。 'app/Config/Paths.php';

          【讨论】:

          • 谢谢,这就是解决方案。
          • 这真的安全吗?
          • 不推荐。您可以将 public 重命名为 public_html。公共文件是“公共”文件,其余的不是。
          • 据我了解,这违背了首先拥有 /public 目录的整个想法。出于安全原因,index.php 文件位于此处。 Nassim 提供的解决方案对我有用。
          【解决方案7】:
          1. 设置虚拟主机(如果您尚未设置),但确保指向 /public 目录(xampp 示例):

          ServerAdmin webmaster@example.com
          ServerName example.com
          DocumentRoot "path-to-project/public"
          <Directory "path-to-project/public">
              Order allow,deny
              Allow from all
              AllowOverride All
              Require all granted
          </Directory>
          

          1. 不要触摸默认 CI4 分发中的任何 .htaccess 文件
          2. 编辑 app/Config/App.php - $baseURL = 'http://www.example.com/'; $indexPage = '';

          【讨论】:

            【解决方案8】:

            我能够在一个项目中“解决”托管服务器中的问题。在我使用更新版本的 codeigniter 4 创建了一个新项目后,我意识到我不能再使用相同的解决方案,经过数小时的研究,我找到了一个非常简单的解决方案。

            我所做的是使用位于项目根文件夹中的 .htaccess 将根目录更改为公用文件夹。

            #RewriteEngine on
            #RewriteCond %{HTTP_HOST} ^mydomain.com$ [NC,OR]
            #RewriteCond %{HTTP_HOST} ^mydomain.com$
            #RewriteCond %{REQUEST_URI} !public/
            #RewriteRule (.*) /public/$1 [L]
            

            使用这个技巧,我可以毫无问题地将我的 codeigniter 4 加载到实时服务器中。 实际上,我用它在子域中配置了一个 codeigniter 4 项目。

            【讨论】:

              【解决方案9】:

              在公共目录之外创建一个名为 index.php 的文件并编写命令

              require_once './public/index.php';
              

              就是这样。

              【讨论】:

              【解决方案10】:

              转到 app/Config/App.php 然后 public $indexPage = 'index.php'; 更改为 public $indexPage = '';

              像魅力一样工作

              【讨论】:

              • 事实上确实如此!坦克
              【解决方案11】:

              更改 index.php =>

              require realpath(FCPATH . 'app/Config/Paths.php') ?: FCPATH . '../app/Config/Paths.php';
              

              require realpath(FCPATH . 'app/Config/Paths.php') ?: FCPATH . 'app/Config/Paths.php';
              

              【讨论】:

                【解决方案12】:

                转到 app/Config/App.php 并找到 public $indexPage = 'index.php';。 从此公共变量中删除 index.php 即可。

                【讨论】:

                【解决方案13】:

                要解决上述从 url 中删除 public/index.php 的问题,您应该按照以下步骤操作:

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

                【讨论】:

                  【解决方案14】:

                  项目结构 codeigniter4


                  If Run project in Xamp show Like this



                  将 public-->index.php 复制到根文件夹-->index.php


                  现在在 webbrowser 中运行 Like this 显示 this like 错误来解决这个问题


                  解决方案

                      $pathsConfig = FCPATH . '../app/Config/Paths.php';
                      to
                     $pathsConfig = FCPATH . 'app/Config/Paths.php';
                  

                  现在运行


                  在根文件夹中创建 .htaccess

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


                  现在您可以在不使用 .htaccess 的情况下切换控制器名称,您的 url 必须像这样 http://localhost:8080/payinlinks/index.php/login 否则 http://localhost:8080/payinlinks/login

                  【讨论】:

                    猜你喜欢
                    • 1970-01-01
                    • 2012-08-10
                    • 2015-10-24
                    • 2015-10-11
                    • 2017-09-28
                    • 2012-12-19
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    相关资源
                    最近更新 更多