【问题标题】:Codeigniter - how to remove the index.php from url?Codeigniter - 如何从 url 中删除 index.php?
【发布时间】:2012-08-10 10:41:41
【问题描述】:

我的项目结构如下

/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

我的项目文件夹中的 index.php 将 user-panel 作为我的应用程序文件夹,而 admin 文件夹中的 index.php 将 admin-panel 作为我的应用程序文件夹。

我已经插入了这一行 $route['default_controller'] = "欢迎"; $route['欢迎'] = '欢迎'; 在用户面板和管理面板中的 routes.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访问我的仪表板控制器

但我不想 index.php 包含在我的网址中。我想删除它。我也尝试过在我的管理文件夹中使用 .htaccess 。我在此写下一行

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

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

【问题讨论】:

  • 你确定在 Apache 中加载了重写模块。
  • 正如我所说,我已将 AllowOverride None 更改为 AllowOverride All 并且我的 .htaccess 在我的管理文件夹中。这是一个正确的位置吗?
  • 尝试将您的 .htaccess 放在项目的根文件夹中
  • 与index.php同目录下
  • 我应该在我的项目文件夹和管理文件夹中创建两个 .htaccess 文件,因为我在两个位置都有 index.php 吗?

标签: php codeigniter


【解决方案1】:

请确保以下几点:

  1. .htaccess 需要和 index.php 文件放在同一目录下,通常是应用的根目录。
  2. read the manual about codeigniter and .htaccess
  3. 确保 apache 正在运行名为:rewrite_module 的重写模块,您可以在此处阅读有关在 linux 上启用该模块的更多信息:Blog post 或此处的 wamp:Wamp 图标 -> Apache -> Apache模块 -> rewrite_module

【讨论】:

  • 你能告诉我我应该在 .htaccess 中写什么吗?
  • 是的。我在您的网址中给出的 .htaccess 中写了这些行
  • RewriteEngine on RewriteCond $1 !^(index\.php|images|robots\.txt) RewriteRule ^(.*)$ /index.php/$1 [L]
  • 我也启用了我的 mod_rewrite 模块
  • 你重启了服务器,所以rewrite模块会激活吗?
【解决方案2】:

看一下config.php文件,里面有个变量叫'index_page'

尝试改变

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

$config['index_page'] = "";

【讨论】:

  • 我的应用程序文件夹不在系统文件夹中,而且我的配置文件夹在我的用户面板文件夹和管理面板文件夹中。
  • 尝试改变 $config['index_page'] = "index.php"; config.php 文件中的值到空白值 $config['index_page'] = " ";还有你已经完成的 .htaccess !
【解决方案3】:

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

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

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

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

并且在 apache 的配置文件中启用了 mod_rewrite。

【讨论】:

    【解决方案4】:

    从 system/application/config 目录打开 config.php

    替换

    $config['index_page'] = “index.php” by $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” 
    

    来自系统/应用程序/config/config.php

    【讨论】:

      猜你喜欢
      • 2015-10-24
      • 2016-05-14
      • 2014-10-28
      • 2012-06-15
      • 2012-09-21
      • 2011-10-12
      • 2017-07-16
      相关资源
      最近更新 更多