01 June 2010 ~ 2 Comments

Codeigniter URLs remove index.php

By default, URLs in CodeIgniter are designed to be search-engine and human friendly. Rather than using the standard “query string” approach to URLs that is synonymous with dynamic systems, CodeIgniter uses a segment-based approach:

example.com/news/article/my_article

A fresh install of Codeigniter will have “index.php” in your urls like so:

example.com/index.php/news/article/my_article

You can easily remove “index.php” by using a .htaccess or adding to your vhost file with some simple rules. Here is an example of such a file, using the “negative” method in which everything is redirected except the specified items. (Note: you must enable rewrite).

RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
Add a CDN link so all images read from the CDN

Most applications now use a CDN “Content Delivery Network” to serve up images to take the load off the web server. You may add this easily to your .htaccess or vhosts file.

RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
 
RewriteRule ^/images/(.*)$ http://url.to.cdn.network.com/$1 [L]

Now all code that is using the path “/images/someimage.gif” will read from “http://url.to.cdn.network.com/someimage.gif”.

For more help with Codeigniter you may want to checkout Professional CodeIgniter
from Amazon.

Tags: ,

2 Responses to “Codeigniter URLs remove index.php”