Example CodeIgniter .htaccess for mod_rewrite

Robin ScottTutorials2 Comments

If you are like Silicon Dales, a few years back, you would have used CodeIgniter (CI) probably quite a lot 5-6 years ago, but perhaps not so much “these days” on projects.

We have a number of clients with legacy CI sites, and sometimes, we need to move them around and configure them.

For this reason, sometimes its necessary to find things like where config.php, database.php files live, and keep a snippet or two around for future reference, because we don’t deal with CI so much these days!

On a recent transfer, as often, the outgoing host had hidden .htaccess files, and we wanted to make sure that the new site had working pretty permalinks, which use mod_rewrite. You may find yourself in a similar situation, so below is a default, simple, CI .htacces file which can help you to make your permalinks mod_rewrite successfully!

.htacces for mod_rewrite using CI


RewriteEngine On
# !IMPORTANT! Set your RewriteBase here and don't forget trailing and leading
# slashes.
# If your page resides at
# http://www.example.com/mypage/test1
# then use
# RewriteBase /mypage/test1/
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

Leave a comment below if this helped you and make us feel a little bit better about our day!

2 Comments on “Example CodeIgniter .htaccess for mod_rewrite”

  1. Working to access a view I created called ‘about_page.php’ by simply typing in localhost/ci/about but currently I can only access it by using localhost/ci/index.php/about. I’ve searched for an answer to this issue, but haven’t been able to search one. Could anyone help for this?

    1. Sounds like a .htaccess issue, based on the need for index.php in the url. Check your rewrite rules are setup right, and also if your hosting supports this.

Leave a Reply

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