Example CodeIgniter .htaccess for mod_rewrite

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

See the code to add in to your .htaccess file in the Gist below.

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
# Blogged @ https://silicondales.com/tutorials/codeigniter-htaccess/
ErrorDocument 404 /index.php
// ENDS HERE!!
view raw .htaccess hosted with ❤ by GitHub

Where to add this

This .htaccess file should be added to the root of your CodeIgniter domain – so if your site lives in a /htdocs/ folder, place it directly into that directory.

This is the standard .htaccess for CI

Please note, I did not invent this. Its the standard .htaccess for CodeIgnitor which was originally submitted by Elliot Haughin. Props to Elliot. All I did was dig it out and post it here for easy future reference.

Not working? Leave a comment

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

2 thoughts 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?

    Reply

Leave a comment

No links of any kind allowed in comments - if you add any links, your comment will automatically be deleted.