CodeIgniter htaccess and URL rewrite issues

Ty Bailey picture Ty Bailey · Feb 9, 2013 · Viewed 179.5k times · Source

I have never used CodeIgniter before, let alone ANY php framework and I thought I would give it a try. Everything is going fine except I cannot seem to remove the index.php from the URL and still access my pages.

I have never used the MVC structure so I am learning as I go, so forgive me if I'm doing this wrong.

I am trying to access a view I created called 'about_page.php' by simply typing in localhost/ci/about but currently I can only access it by using localhost/ci/index.php/about

The controller for the page is: /application/controllers/about.php
The Model for the page is: /application/models/about_model.php
And the View for the page is: /application/views/about_page.php

I have searched for a solution to this issue, but haven't been able to find one. Here is where I have already searched:

CodeIgniter - removing index.php
Codeigniter - how to remove the index.php from url?
http://www.farinspace.com/codeigniter-htaccess-file/

CodeIgniter comes with a .htaccess file in the 'application' folder which contains only Allow Deny From All. So I created a new .htaccess file in the root directory, http://localhost/ci/.htaccess and added this code to it:

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

When the .htaccess file is in the root directory I get a 500 Internal Server Error. When I copy the exact same file into the applications folder the 500 error goes away, but I still cannot access the about page by using localhost/ci/about

I have already changed $config['index_page'] = 'index.php'; to $config['index_page'] = ''; AND I tried changing $config['uri_protocol'] = 'AUTO'; to $config['uri_protocol'] = 'REQUEST_URI'; but I am still getting the Internal Server Error.

I went into the httpd.conf file and uncommented the mod_rewrite.so module so I know mod_rewrite is active.

Does anyone have any ideas why this isn't working or how I can get this work? I know there are alot of questions on StackOverflow on this subject but I couldn't find one that answered my question.

Am I doing this right? Should I even be able to access the about page by visiting localhost/ci/about or do I have to create an 'about' directory in the 'application' directory?

Answer

Rahul Chipad picture Rahul Chipad · Feb 11, 2013

There are 3 steps to remove index.php.

  1. Make below changes in application/config.php file

    $config['base_url'] = 'http://'.$_SERVER['SERVER_NAME'].'/Your Ci folder_name';
    $config['index_page'] = '';
    $config['uri_protocol'] = 'AUTO';
    
  2. Make .htaccess file in your root directory using below code

    RewriteEngine on
    RewriteCond $1 !^(index\.php|resources|robots\.txt)
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1 [L,QSA]
    
  3. Enable the rewrite engine (if not already enabled)

    i. First, initiate it with the following command:

    a2enmod rewrite
    

    ii. Edit the file /etc/apache2/sites-enabled/000-default

    Change all AllowOverride None to AllowOverride All.

    Note: In latest version you need to change in /etc/apache2/apache2.conf file

    iii. Restart your server with the following command:

    sudo /etc/init.d/apache2 restart