代码点火器 - 如何从 url 中删除索引.php


Codeigniter - how to remove the index.php from url?

>我的项目有以下结构

/system
/applications
  /cache
  /core
  /helpers
  /hook
  /language
  /libraries
  /logs
  /third_party
  /admin-panel
     /config
     /controllers
        welcome.php
        dashboard.php
     /errors
     /models
     /views
        welcome.php
        dashboard.php
  /user-panel
     /config
     /controllers
            welcome.php
            dashboard.php
     /errors
     /models
     /views
        welcome.php
        dashboard.php
/admin
  index.php
index.php

我的索引.php在我的项目文件夹中将用户面板作为我的应用程序文件夹和索引.php在管理文件夹中将管理员面板作为我的应用程序文件夹。

我已经插入了这一行$route['default_controller'] = "欢迎";$route['欢迎'] = '欢迎';在路由中.php在用户面板和管理面板中

我可以使用http://localhost/myproject and http://localhost/myproject/admin访问我的用户面板和管理面板的欢迎控制器

但我无法使用 http://localhost/myproject/dashboard or http://localhost/myproject/admin/dashboard 访问我的仪表板控制器

可以使用http://localhost/myproject/index.php/dashboard or http://localhost/myproject/admin/index.php/dashboard访问我的仪表板控制器

但我不想在我的网址中包含索引.php。我想删除它。我也尝试在我的管理文件夹中使用 .htaccess。我在这里写下以下行

RewriteEngine On
RewriteCond $1 !^(index'.php|(.*)'.swf|robots'.txt)
RewriteRule ^(.*)$ index.php/$1 [L]

但这对我不起作用。它给出 404 未找到 CI 错误。我还通过在 httpd.conf 中将 AllowOerride None 更改为 AllowOverride All 来启用mod_rewrite。请告诉我应该怎么做才能删除索引.php以及我应该将 .htaccess 文件放在项目目录中的什么位置。

如果您的项目的根文件夹不是域的根目录,即您的网站是域http://localhost/myproject的子目录,那么您需要在.htaccess文件中增加一行RewriteBase

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

还要确保您的配置.php配置为

$config['base_url'] = 'http://localhost/myproject/';
$config['index_page'] = '';

mod_rewrite在 Apache 的配置文件中启用。

打开配置.php 从系统/应用程序/配置目录

并替换

$config['index_page'] = "index.php" x $config['index_page'] = "

在 CodeIgniter 目录的根目录中创建一个 ".htaccess" 文件

并添加以下行。

    RewriteEngine on
RewriteCond $1 !^(index'.php|resources|robots'.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA] 

在某些情况下,uri_protocol的默认设置无法正常工作。

要解决此问题,只需更换

`$config['uri_protocol'] = “AUTO”` 

 $config['uri_protocol'] = “REQUEST_URI” 

从系统/应用程序/配置/配置.php

确保以下几点:

  1. .htaccess需要放在与index.php文件相同的目录中,通常是应用程序的根目录。
  2. 阅读有关 Codeigniter 和 .htaccess 的手册
  3. 确保 apache 正在运行名为 的重写模块: rewrite_module您可以在此处阅读有关在 Linux 上启用该模块的更多信息: 博客文章或此处的 wamp : Wamp icon -> Apache -> Apache 模块 -> rewrite_module

看看 config.php 文件中,有一个名为 'index_page' 的变量

尝试更改

$config['index_page'] = "index.php";

$config['index_page'] = "";