CodeIgniter routes and .htaccess

CodeIgniter has a robust routing module as part of its MVC architecture. A trap for the inexperienced arises when the default controller works (e.g. but anything else such as goes to a server based 404, not the CodeIgniter one.

Solution: in your config.php file ensure this line is present
$config['index_page'] = '';
and place the following code in an .htacess file
Options -Indexes
Options +FollowSymLinks

# Set the default file for indexes
DirectoryIndex index.php

<IfModule mod_rewrite.c>

# activate URL rewriting
RewriteEngine on

# do not rewrite links to the documentation, assets and public files (your folders here)
RewriteCond $1 !^(images|assets|uploads|js)

# do not rewrite for php files in the document root, robots.txt or the maintenance page etc
RewriteCond $1 !^([^\..]+\.php|robots\.txt|maintenance\.html)

# rewrite everything else
RewriteRule ^(.*)$ index.php/$1 [L]

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

ErrorDocument 404 index.php


One thought on “CodeIgniter routes and .htaccess”

Leave a Reply

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