Removing index.php from CodeIgniter’s Segment-Based URL

I tried to follow the directions in the CI Users Guide for Removing the index.php file from the URLs. It didn’t work! Here I will document my effort to accomplish this goal. If you are reading this—and don’t see a full post—it’s because this is a work in progress. I update this page as I go.

The CI UG says:

You can easily remove this file by using a .htaccess file with some simple rules. Here is an example of such a file, using the “negative” method in which everything is redirected except the specified items:

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

In the above example, any HTTP request other than those for index.php, images, and robots.txt is treated as a request for your index.php file.

Okay! it sounds so simple. Doesn’t it?

They don’t tell you which .htaccess file to add these lines to.

So, I tried putting them in a .htaccess file I created from scratch in my CodegIniter project main directory — /Users/samehlabib/Sites/Sites/CodeIgniter_2.1.0/

Anyways, I tested by browsing to my CI project directory. My browser took me to the index.php of my older web project which is located at /Users/samehlabib/Sites/Sites/web/

After some further investigation I noticed that my http root ( /Users/samehlabib/Sites/Sites/) has an index.php file which does a redirect to the old web project I saw in the browser.

I just had a Eureka moment!

I think the .htaccess code in the UG needs to be adapted to my situation as follows—and I will still put it in the project directory:

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

Hooray! It worked!!!

Just so you know, there is another set of directives out there in the wild which are used for this same purpose. They look like the following:

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

Just like the set I’m using now, these will likely need to be adapted to the particulars of my situation. For example I think they have to be placed in an .htaccess file in the same directory as the CI index.php page; And, lines 2 and 5 will have to include the path from webroot to the CI directory.


About samehramzylabib

See About on
This entry was posted in CodeIgniter, Configure My Systems. Bookmark the permalink.


Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s