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?

Community
  • 1
  • 1
jezzipin
  • 4,110
  • 14
  • 50
  • 94

2 Answers2

3

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.

e.g. 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

stormdrain
  • 7,915
  • 4
  • 37
  • 76
  • Thanks for your response. I understand what you mean with regards to removing the controller from the URL as it could potentially lead to a loss of scope during development. However, if this is the case how do I go about linking between my views? If I use site/home or site/about it's fine for the first time I click one of them but the next time around it links to site/site/about. – jezzipin Apr 25 '13 at 14:07
  • 1
    Check `$config['base_url']` in application/config.php and set it. – stormdrain Apr 25 '13 at 14:10
  • In this case i'm guessing it's localhost/ci_intro ? – jezzipin Apr 25 '13 at 14:11
  • 2
    Well, actually, that's probably not the problem. Issue is that the link href="home" will link to ci_intro/home as expected, but CI is then looking for a home controller which doesnt exist. You could add a route in config/routes.php like `$route['about'] = 'site/about';` and the same for home. – stormdrain Apr 25 '13 at 14:13
  • Is that the standard CodeIgniter approach? Seems very long-winded just to get a couple of links working. – jezzipin Apr 25 '13 at 14:15
  • 1
    No! This is the point of my answer. Better to create a `home` controller and an `about` controller :) – stormdrain Apr 25 '13 at 14:15
  • I see what you mean now. I'm following a tutorial just to get to grips with the basics of CI (as I've coded in vanilla PHP for years) and they just seem to put a home and an about function under a class of site which they then use to load the views and models. Are you suggesting I need a new class called About and another called Home? – jezzipin Apr 25 '13 at 14:18
  • 1
    There are many ways to skin a rock. If it were me (and I think this is the typical CI way), I would create a controller for each page, so yes. I've updated the answer with example url's and an example `about` controller. – stormdrain Apr 25 '13 at 14:21
  • Great, thanks. That's what made the most sense to me but when I was watching the tutorial and they didn't follow that approach I became a but skeptical. Thankyou very much for your clear explanation. – jezzipin Apr 25 '13 at 14:23
  • 1
    The line `RewriteRule ^(.*)$ /index.php/$1 [L]` Wasn't working for me, until I removed the / before index.php. That is: `RewriteRule ^(.*)$ index.php/$1 [L]` – Brandon Romano Sep 29 '13 at 21:40
  • @BrandonRomano yeah, it depends on where the codeigniter installation is. If it is in a subfolder like the OP's was, you're right, there shouldn't be a slash. If the `.htaccess` is in the webroot, then a forward slash is OK (but not necessary). – stormdrain Sep 30 '13 at 13:13
  • Yup, thanks for specifying the difference, I should have been more explicit in my post of why I changed it. Thanks for the informative post @stormdrain, I was banging my head into a wall over this issue for a while. – Brandon Romano Oct 01 '13 at 02:38
2

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';
Diego Castro
  • 2,046
  • 2
  • 21
  • 28