如何从 Codeigniter 4 中的 url 中删除 public/index.php/

cav*_*boy 15 php codeigniter-4

我想要像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]
Run Code Online (Sandbox Code Playgroud)

我的 .htaccess 文件

小智 21

做就是了

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

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

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

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

  • 根据我的理解,这违背了首先拥有 /public 目录的整个想法。出于安全原因,index.php 文件位于此处。纳西姆提供的解决方案对我有用。 (4认同)
  • 不推荐。您可以将 public 重命名为 public_html。公开的文件是“公开”文件,其余的不是。 (3认同)

Nas*_*sim 12

好的,这就是我解决这个问题的方法:您需要 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]
Run Code Online (Sandbox Code Playgroud)

第二个(与 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>
Run Code Online (Sandbox Code Playgroud)


小智 5

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

像魅力一样工作


Abd*_*lam 1

我不知道你是怎么得到这个的.htaccess。如果您从这里下载 CodeIgniter 4,您可以看到Htaccess文件与您添加的不同。

由于 CodeIgniter (1,2,3) 到 Codeigniter 4 有许多更改,htaccess 移动到public/设置为文档根目录的文件夹,而外部.htaccess的位置在 CI 4 中无效


正确路径为.htaccess

public/.htaccess
Run Code Online (Sandbox Code Playgroud)

注意:我刚刚测试了index.phpURL 问题,没有出现类似的问题。工作/加载良好,无需index.php