簡體   English   中英

如何從 Codeigniter 4 中的 url 中刪除 public/index.php/

[英]How to remove public/index.php/ from url in Codeigniter 4

我想要正常的 URL 像www.sample.com/controller而不是www.sample.com/public/index.php/controller

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

我的 .htaccess 文件

就做

第一步:在 public/ 目錄下,將 index.php 和 .htaccess 復制到你的項目根目錄。

第二步:在項目根目錄下,打開 index.php 並編輯以下行:

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

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

好的,這就是我解決這個問題的方法:你需要 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>

轉到app/Config/App.php然后public $indexPage = 'index.php'; 更改為公共 $indexPage = '';

像魅力一樣工作

在公共目錄外創建一個名為 index.php 的文件並編寫命令

require_once './public/index.php';

就是這樣。

  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 = '';

我不知道你是怎么得到這個.htaccess 如果您從這里下載 CodeIgniter 4 ,您會看到Htaccess文件與您添加的不同。

由於 CodeIgniter (1,2,3) 對 Codeigniter 4 進行了許多更改,因此htaccess 移至public/文件夾,該文件夾設置為文檔根目錄.htaccess在 CI 4 之外的位置將無效


.htaccess正確路徑

public/.htaccess

注意:我剛剛測試了index.php URL 問題,沒有類似的問題。 在沒有index.php情況下工作/加載良好

我想要普通網址,例如www.sample.com/controller而不是www.sample.com/public/index.php/controller

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

我的.htaccess文件

/public 中創建一個.htaccess文件:

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

第一步:在 public/ 目錄下,將 index.php 和 .htaccess 復制到你的項目根目錄。

第二步:在項目根目錄下,打開 index.php 並編輯以下行:

改變:

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

更改為

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

在此處輸入圖片說明

你有沒有試過這個:

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

我能夠在一個項目中“解決”托管服務器中的問題。 在我使用較新版本的 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 項目。

更改 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';

轉到 app/Config/App.php 並找到public $indexPage = 'index.php'; . 從這個公共變量中刪除index.php就是這樣。

要解決從 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();

項目結構 codeigniter4


項目結構 codeigniter4

如果在 Xamp 中運行項目顯示像這樣


在此處輸入圖片說明


復制public-->index.php到root文件夾-->index.php 在此處輸入圖片說明


現在在 webbrowser 中運行像這樣顯示這個像錯誤來解決這個問題在此處輸入圖片說明


解決方案在此處輸入圖片說明

    $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 else http://localhost:8080/payinlinks/login 在此處輸入圖片說明

步驟1:

  • Go 進入 Your_Project_Folder > App > Config > App.php
  • 更改為 $baseURL='http://localhost/Your_Project_Name'
  • 更改為 $indexPage=""

在此處輸入圖像描述

第2步:

  • Go 進入 Your_Project_Folder > Public

  • 復制.httaccessindex.php

  • 將這些文件粘貼到 Your_Project_Folder

在此處輸入圖像描述

第 3 步:

  • Go 進入 Your_Project_Folder > index.php

  • 改變, FCPATH。 '../app/Config/Paths.php'FCPATH。 '應用程序/配置/Paths.php'

在此處輸入圖像描述

全部完成!!!

在 CodeIgniter 4.2 或更高版本中,您必須遵循3 steps

1st Step: Go 到這個文件夾

應用程序 -> 配置 -> app.php

使用your project folder name更新base_url ,就像我們在Codeigniter 3中所做的那樣:

public $baseURL = 'http://localhost/your_project_name/';

如果使用composer設置ci 4 ,那么您將擁有這種類型的base_url ,您需要使用上面定義的base_url進行更改:

public $baseURL = 'http://localhost:8080';

2nd Step: Go 到public文件夾並copy index.php.htaccess文件(記住復制它們不要在這里剪切它們)然后將它們pasteroot目錄

注意:與我們刪除index.php Codeigniter 3中的 index.php 相同,方法是在根文件夾中創建.htaccess文件並將相關代碼paste到 `.htaccess' 文件中。

3rd Step:root文件夾.htaccess文件代碼替換為:

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

然后打開root文件夾index.php文件並檢查第20行或這一行:

require FCPATH . '../app/Config/Paths.php';

並像這樣從中刪除../

require FCPATH . 'app/Config/Paths.php';

In case you created css js folder in public folder and connected them using base_url on views/header.php views/footer.php or views/index.php then might be it occurs problems and don't get these files because of related problems:

當我們使用bootstrap cdn時,它給出了我們這樣本地化的syntax ,但是由於id="bootstrap-style"這會出現問題,因此請從該行刪除此表單:

<link href="<?php echo base_url();?>/public/assets/css/bootstrap.min.css" id="bootstrap-style" />

還形成其他css linked文件刪除此type="text/css"或任何id=""它會出現問題。

添加css鏈接的正確方法是在鏈接上定義rel="stylesheet" ,如下所示:

<link rel="stylesheet" href="<?php echo base_url();?>/public/assets/css/bootstrap.min.css" />

就我而言,我遇到了與bootstrapcss相關的問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM