site map

Parkside Web Development

Read... Write... Share...

July 14, 2008

Pretty URLS With Code Igniter & Mod Rewrite

Filed under: Developing the WebThe Quagmire @ 6:43 am

Code Igniter does a pretty good job of creating pretty urls by default. The basic Code Ignitor address will look like this:

http://www.example.com/index.php/controller/method/parameter

What we want to do is get that index.php out of there. We’ll accomplish this by creating an .htaccess file in our root directory and use it to rewrite the address with Apache’s Mod Rewrite Engine.

Here’s a sample .htaccess file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<ifModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
 
    #We want to prevent users from
    #accessing our system folder
    RewriteCond %{REQUEST_URI} ^/system.*
    RewriteRule ^(.*)$ /index.php/$1 [L]
 
    #Next we want visitors to be able
    #to access real files on our site
    #such as images or css files.
    RewriteCond %{REQUEST_FILENAME} !-f
 
    #We also want users to be able to
    #get into directories that may contain
    #extra files or a separate application
    #for example /resources or /blog.
    RewriteCond %{REQUEST_FILENAME} !-d
 
    #Finally we want to send everything
    #after the domain name to Code
    #Igniter's index.php for processing.
    RewriteRule ^(.*)$ index.php/$1 [L]
</ifModule>

Next we need to let Code Igniter know that it shouldn’t put index.php in any of it’s urls. We do that by modifying the configuration file at: system/application/config/config.php. We want to find the line of code that looks like this:

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

And change it to look like this:

1
$config['index_page'] = "";

You’ll also have to ensure that Apache is properly configured. Make sure that your apache configuration file contains the load module command:

1
LoadModule rewrite_module /usr/lib/apache2/modules/mod_rewrite.so

AND that your directory is set up to allow .htaccess directives:

1
2
3
4
<directory "/path/to/htdocs">
    Options FollowSymLinks
    AllowOverride FileInfo
</directory>

Once these steps are complete, your Code Igniter installation should be able to process urls that don’t contain index.php.

Share/Save/Bookmark


6 Comments »

bad land — June 19, 2008 @ 6:11 am

Nice Site!

John — July 7, 2008 @ 11:52 pm

Good tip. One thing though, you forgot the ‘/’ in one of your rules which essentially nulls the rule.

It should be:
RewriteCond %{REQUEST_URI} ^/system.*

Keep up the good work! I just started using CI as well and I love it. It’s so much more flexible than a lot of other frameworks around. I can add and extend components with ease.

happy coding!
j

The Quagmire — July 8, 2008 @ 10:14 am

Thanks for the correction John. I agree that CI is great. I use it for just about every project now. It makes it super easy to kick off a project.

chris — July 13, 2008 @ 9:18 am

thanks a lot. very helpful

Darin Boyd / Braingerous — January 9, 2009 @ 1:10 pm

Nice article. I had wondered how you would do something like that. I’m still getting into the LAMP stuff after years on IIS servers.

The Quagmire — January 10, 2009 @ 12:45 am

Thanks Darin. Good to have you in LAMP world. I spent quite a few years doing development with IIS servers and couldn’t stand it.

RSS feed for comments on this post. TrackBack URL

Leave a comment