1

I'm having some trouble using Codeigniter2. Essentially I want to remove the annoying "index.php" from my url so that:

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

    Becomes

    - http://localhost/ci_intro/home

I've followed a couple of the links on Stack Overflow already and have found the following two posts:

Remove index.php From URL - Codeigniter 2

And

Removing index.php from codeigniter-2

However after performing both steps still no joy.

My .htaccess is as follows:

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

I've ensured that the mod_rewrite module is enable in Apache (I'm using Xampp on Windows) but for some reason it's just not working as i'd expect it to.

I have two views in CodeIgniter2, one called Home and another called About. These two link to each other. The links are as follows:

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

If I go to the root URL of my project:

   http://localhost/ci_intro/

The index function of the Home page is displayed. That's fine and as expected. However, if I then click either link it redirects to:

   http://localhost/ci_intro/home

   or

   http://localhost/ci_intro/about

Which produces a 404. However if I then type "site" in the URL, the name of my controller, the links take me through to the correctly loaded views:

    http://localhost/ci_intro/site/home

    or

    http://localhost/ci_intro/site/about

Is there any known way of bypassing this approach so that the controller,in this case site, is removed from the URL entirely as using site/home or site/about in the links in my view does not appear to work?

4

2 回答 2

3

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

- 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 之前)的 url 将显示一个带有文本“我是家庭控制器索引”的页面。

然后您可以将其他功能添加到家庭控制器 a'la

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";
    }
}

带有类似 urlhttp://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,则可以使用路由

于 2013-04-25T14:03:59.150 回答
2

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

它看起来像这样:

$route['home'] = 'site/home';
$route['about'] = 'controllername/about';
于 2013-04-25T21:46:05.520 回答