URL Rewriting in CodeIgniter

As mentioned on EllisLab website, CodeIgniter is a powerful PHP framework with a very small footprint, built for PHP coders who need a simple and elegant toolkit to create full-featured web applications. At the same time, there are few simple things which may stall beginners with this framework.

I faced a problem with URL rewriting in CodeIgniter experiment and with the help of community found a solution as well so posting it as a blog post for everyone’s help so that you don’t need to find the whole internet for this simple solution 🙂

Problem: Removing index.php file from codeigniter URL and adding .html suffix to it:
1. Create an .htaccess file in the root directory of CodeIgniter installation
2. Make sure to double check .htaccess extension as notepad might turn it as txt file when saved in default mode so select “All Files” option while saving it as .htaccess file
3. Add the following rules to .htaccess file to have URL rewriting option ready to remove index.php file from URL:

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

4. Then find the following line in your application/config/config.php file:

$config['index_page'] = 'index.php';

5. Set the variable empty as below:

$config['index_page'] = '';

6. If it still doesn’t work, find the following line in your application/config/config.php file: and try to replace following variable with these parameters. For me, AUTO option worked after having above steps:
(‘AUTO’, ‘PATH_INFO’, ‘QUERY_STRING’, ‘REQUEST_URI’, and ‘ORIG_PATH_INFO’)

$config['uri_protocol'] = 'AUTO';

7. To add .html suffix, find following line in your application/config/config.php file and add .html as default suffix:

$config['url_suffix'] = '.html';

That’s all. Here you go 🙂 – good luck!

Leave a Reply

Your email address will not be published. Required fields are marked *