You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

CodeIgniter 4无法移除URL中的index.php,求解决方法

问题:CodeIgniter 4无法移除URL中的index.php(怀疑与Vagrant有关)

我试过网上所有教程来移除CodeIgniter 4 URL中的index.php,但完全没用,怀疑问题出在Vagrant上。

当前配置详情

Apache配置文件

<Directory /var/www/>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
</Directory>

.htaccess文件

我按照教程增删过各种内容,但都无效。所有教程都说只要包含rewriteRule (.*)$ index.php/$1 [L]就应该管用,但实际没效果。当前文件内容:

# 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 /

    # Redirect Trailing Slashes...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} (.+)/$
    RewriteRule ^ %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 ^([\s\S]*)$ index.php/$1 [L,NC,QSA]

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

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

</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

已调整的文件位置

  • 将.htaccess和index.php从项目的public文件夹移到了项目主目录

App.php相关配置代码

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

/**
 * --------------------------------------------------------------------------
 * 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.
 *
 * @var string
 */
public $indexPage = '';

/**
 * --------------------------------------------------------------------------
 * URI PROTOCOL
 * --------------------------------------------------------------------------
 *
 * This item determines which server 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!
 *
 * @var string
 */
public $uriProtocol = 'REQUEST_URI';

index.php的修改

  • 将原代码require FCPATH . '../app/Config/Paths.php'修改为require FCPATH . '/app/config/Pathos.php'

内容的提问来源于stack exchange,提问作者Dylano236

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.02 03:05:49