Guide to CodeIgniter databases

Why are there no, in-depth tutorials covering databases in CodeIgniter?

I?m talking the full nine miles, including Models, ActiveQuery, Configuration, Security, seriously, the full nine miles. Yes the CodeIgniter user guide is spectacular, yes, most people know how to do this stuff anyway, but, I did what I could. I would like to introduce you to?

The CodeIgniter Guide To Databases.

Mod_rewrite: RewriteCond

For a plain English translation, go down to the end…

Syntax: RewriteCond TestString CondPattern

The RewriteCond directive defines a rule condition. Precede a RewriteRule directive with one or more RewriteCond directives. The following rewriting rule is only used if its pattern matches the current state of the URI and if these additional conditions apply too.

TestString is a string which can contains the following expanded constructs in addition to plain text:
– RewriteRule backreferences
– RewriteCond backreferences
– RewriteMap expansions
– Server-Variables

Note that this is a simplified version of what’s at
which goes on for several pages about this in vast detail.

To put it into plain English:
Say you have this (as can be found in a CodeIgniter .htaccess file):
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php/$1 [L]

What this means is:
IF the incoming Request URI (ie the address requested by the browser) matches “system”
THEN rewrite it so that it uses the index.php file in the root directory.

For explanation of RewriteRule see an earlier post.

Mod_rewrite: RewriteBase

This basically says:
We are in the directory “/abc” but please process all Rewrites as if you were in a root directory (i.e. ignore the directory /abc).

What it does is strip this directory off before processing further rules then add it back on at the end.

From the Apache docs:
The RewriteBase directive explicitly sets the base URL for per-directory rewrites. As you will see below, RewriteRule can be used in per-directory config files (.htaccess). There it will act locally, i.e., the local directory prefix is stripped at this stage of processing and your rewriting rules act only on the remainder. At the end it is automatically added back to the path.

Getting rid of index.php from your CodeIgniter URLs

Here are the steps you need to follow:
1. Create a .htaccess file to configure the rewrite engine (an example htaccess file follows at the end)

2. Set $config[?index_page?] to an empty string

3. Make sure your apache uses the mod_rewrite module

4. Make sure apache is configured to accept needed .htaccess directives

5. Restart apache and test

RewriteEngine On
RewriteBase /

#Removes access to the system folder by users.
#Additionally this will allow you to create a System.php controller,
#previously this would not have been possible.
#’system’ can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php/$1 [L]

#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn’t true it sends the
#request to index.php
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



Syntax: RewriteRule Pattern Substitution

Here’s an example you could use in a .htaccess file:
RewriteRule ^(.*)$ /index.php/$1 [L]
which says if you have a URL like this:
then replace it with

where the not very user-friendly [L] flag means “stop processing”:
‘last|L’ (last rule)
Stop the rewriting process here and don’t apply any more rewriting rules. This corresponds to the Perl last command or the break command from the C language. Use this flag to prevent the currently rewritten URL from being rewritten further by following rules. For example, use it to rewrite the root-path URL (‘/’) to a real one, e.g., ‘/e/www/’.

See here for more details:

Creating Japanese CSV files that Excel can read…

Here’s a test script…

These pages are helpful:
(see Smelly’s comment 26-Apr-2007 06:09)

See also: