Codeigniter routing and caching using htaccess

To remove the index.php from the uri (SEO) and improve performance / caching you can create a .htaccess file. This file redirects the page request to the framework and handles the caching.

Using shell access, first enable modrewrite in apache

$ sudo a2enmod rewrite
$ sudo /etc/init.d/apache2 restart
$ sudo service apache2 restart

Using shell access, update the virtualhosts file (etc/apache/sites-enabled/000-default). In the section set: AllowOverride all

$ sudo vi etc/apache/sites-enabled/000-default

When using a .htaccess file, this should be placed in your DocumentRoot. IMPORTANT: Set your RewriteBase here and don't forget trailing and leading slashes. If your page resides at then use RewriteBase /mypage/test1/

.htaccess example:


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

# If we don't have mod_rewrite installed, all 404's
# can be sent to index.php, and everything works as normal.
# Submitted by: ElliotHaughin
ErrorDocument 404 /index.php


ExpiresActive On
ExpiresByType image/jpg "access 1 month"
ExpiresByType image/jpeg "access 1 month"
ExpiresByType image/gif "access 1 month"
ExpiresByType image/png "access 1 month"
ExpiresByType text/css "access 1 week"
ExpiresByType text/html "access 1 week"
ExpiresByType application/pdf "access 1 month"
ExpiresByType text/x-javascript "access 1 week"
ExpiresByType application/x-shockwave-flash "access 1 month"
ExpiresByType image/x-icon "access 1 year"
ExpiresDefault "access 1 month"

why-guy add:

Last Tweets: