简体   繁体   中英

Removing index.php in Codeigniter 2

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?

You probably don't want to do that (but might!).

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

The way CI works, site is the controller and home is the method or function in the controller you are calling. This allows you to gather respective functionality.

eg you could have a home controller and an index method like:

class Home extends CI_Controller{

    public function index(){
        echo 'I am the home controller index';
    }
}

which with a url like http://example.com/home (or http://example.com/index.php/home before .htaccess is involved) would show a page with the text "I am the home controller index".

Then you can add other functionality to the home controller 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";
    }
}

which with a url like http://example.com/home/user would show a page with the text "I am the user method in the home controller".

You can also create as many other controllers as you need:

class About extends CI_Controller{

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

http://example.com/about will render a page with the text "I am the about page!"

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

is all the .htaccess you should need to remove index.php form the url. It'd live in /ci_into/.htaccess

If you really want to use http://example.com/home that works with the site controller, you could use routing

Use routes: http://ellislab.com/codeigniter/user-guide/general/routing.html

It will look something like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM