【问题标题】:Removing index.php in Codeigniter 2在 Codeigniter 2 中删除 index.php
【发布时间】:2013-04-19 10:47:56
【问题描述】:

我在使用 Codeigniter2 时遇到了一些问题。 本质上,我想从我的网址中删除烦人的“index.php”,以便:

    - http://localhost/ci_intro/index.php/site/home

    Becomes

    - http://localhost/ci_intro/home

我已经关注了 Stack Overflow 上的几个链接,并找到了以下两个帖子:

Remove index.php From URL - Codeigniter 2

Removing index.php from codeigniter-2

但是在执行完这两个步骤之后仍然没有乐趣。

我的.htaccess如下:

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /ci_intro/

    #Removes access to the system folder by users.
    #Additionally this will allow you to create a System.php controller,
    #previously this would not have been possible.
    #'system' can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /ci_intro/index.php?/$1 [L]

    #When your application folder isn't in the system folder
    #This snippet prevents user access to the application folder
    #Submitted by: Fabdrol
    #Rename 'application' to your applications folder name.
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /ci_intro/index.php?/$1 [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 index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /ci_intro/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.
    # Submitted by: ElliotHaughin

    ErrorDocument 404 /index.php
</IfModule>   

我已确保在 Apache 中启用了 mod_rewrite 模块(我在 Windows 上使用 Xampp),但由于某种原因,它并没有像我预期的那样工作。

我在 CodeIgniter2 中有两个视图,一个叫做 Home,另一个叫做 About。这两者是相互联系的。链接如下:

    <a href="home">Home</a><br/>
    <a href="about">About</a>

如果我转到我的项目的根 URL:

   http://localhost/ci_intro/

首页的索引功能显示。这很好,正如预期的那样。但是,如果我然后单击其中一个链接,它会重定向到:

   http://localhost/ci_intro/home

   or

   http://localhost/ci_intro/about

这会产生 404。但是,如果我随后在 URL 中键入“站点”,即我的控制器的名称,则链接会将我带到正确加载的视图:

    http://localhost/ci_intro/site/home

    or

    http://localhost/ci_intro/site/about

是否有任何已知的方法绕过这种方法,以便控制器(在这种情况下为站点)从 URL 中完全删除,因为在我的视图中的链接中使用 site/home 或 site/about 似乎不起作用?

【问题讨论】:

    标签: php .htaccess codeigniter mod-rewrite codeigniter-2


    【解决方案1】:

    您可能不想这样做(但可能!)。

    - http://localhost/ci_intro/index.php/site/home
    
    Becomes
    
    - http://localhost/ci_intro/home
    

    CI 的工作方式,site 是控制器,home 是您正在调用的控制器中的方法或函数。这使您可以收集相应的功能。

    例如你可以有一个家庭控制器和一个索引方法,比如:

    class Home extends CI_Controller{
    
        public function index(){
            echo 'I am the home controller index';
        }
    }
    

    带有http://example.com/home(或http://example.com/index.php/home,在涉及.htaccess 之前)之类的网址会显示一个带有“我是家庭控制器索引”文本的页面。

    然后你就可以给home控制器添加其他功能啦

    class Home extends CI_Controller{
    
        public function index(){
            // http://example.com/home
            echo 'I am the home controller index';
        }
    
        public function user(){
            // http://example.com/home/user
            echo "I am the user method in the home controller";
        }
    }
    

    带有http://example.com/home/user 之类的网址会显示一个带有“我是家庭控制器中的用户方法”文本的页面。

    您还可以根据需要创建任意数量的其他控制器:

    class About extends CI_Controller{
    
        public function index(){
            // http://example.com/about
            echo 'I am the about page!';
        }
    }
    

    http://example.com/about 将呈现一个带有文本“我是关于页面!”的页面

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

    是您需要从 url 中删除 index.php 的所有 .htaccess。它会住在/ci_into/.htaccess

    如果你真的想使用与站点控制器配合使用的http://example.com/home,你可以使用routing

    【讨论】:

    • 在 application/config.php 中检查 $config['base_url'] 并设置它。
    • 嗯,实际上,这可能不是问题所在。问题是链接 href="home" 将按预期链接到 ci_intro/home,但 CI 正在寻找一个不存在的家庭控制器。您可以在 config/routes.php 中添加一个路由,例如 $route['about'] = 'site/about'; 和 home 一样。
    • 不!这就是我回答的重点。最好创建一个home 控制器和一个about 控制器:)
    • 剥石头皮的方法有很多种。如果是我(我认为这是典型的 CI 方式),我会为每个页面创建一个控制器,所以是的。我已经用示例 url 和示例 about 控制器更新了答案。
    • RewriteRule ^(.*)$ /index.php/$1 [L] 行对我不起作用,直到我删除了 index.php 之前的 /。即:RewriteRule ^(.*)$ index.php/$1 [L]
    【解决方案2】:

    使用路由:http://ellislab.com/codeigniter/user-guide/general/routing.html

    它看起来像这样:

    $route['home'] = 'site/home';
    $route['about'] = 'controllername/about';
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-07-09
      • 1970-01-01
      • 2012-03-28
      • 2014-09-20
      • 1970-01-01
      • 1970-01-01
      • 2016-07-26
      相关资源
      最近更新 更多