Monday, February 14, 2011

Using CodeIgniter and Apache Rewrite for Pretty URLs

After spending two hours trying different configurations I finally found the one that worked for me. I found no shortage of help online but none helped. This is what finally worked for me.

Changed my CodeIgniter config, application/config/config.php

From this: $config['index_page'] = 'index.php';
To this: $config['index_page'] = '';

From this: $config['uri_protocol'] = 'AUTO';
To this: $config['uri_protocol'] = 'REQUEST_URI';


After I enabled rewrite_module I made a .htaccess file in my www root.


<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /ignite_tonys/
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>


<IfModule !mod_rewrite.c>
    ErrorDocument 404 /index.php
</IfModule>  


There it is. Now I have shorter URIs and I don't have to see the /index.php/ in any of the URIs in my application.