URL Rewriting for CodeIgniter
October 12, 2010

What is URL Rewriting?

URL rewriting provides shorter and more relevant-looking links to web pages on your site. This improves the readability and the search rankings of your URLs. For example, URL "a" can be rewritten as URL "b".

a) http://example.com/index.php?section=casting
b) http://example.com/casting

There are many articles on the web discussing the benefits of shorter and more relevant URLs. Let me add to them that you get to hide the used technology from both search engines and users. You'll also have a better time migrating to a different platform in case you need to. For example, if you build your web application on top of ASP.NET and use URL Rewriting, you can easily migrate to PHP (Recommended, of course) without changing your links and hence without losing all the search-ranking score for those links.

CodeIgniter Default URLs

By default, CodeIgniter uses a segment-based approach to represent URLs. Unfortunately, CodeIgniter includes the annoying "index.php" file name in the URL. For example:

http://example.com/index.php/products/view/shoes

Now, the CodeIgniter manual mentions that it's very simple to remove the "index.php" part from the URL using the following .htaccess file:

RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]

Problem solved? Not really. This didn't work on my local machine nor on my online hosting account (Dreamhost). Why? I don't know. I don't care. I don't currently have the time to find out why.

Having already installed WordPress, I remembered that their .htaccess file works offline and online (at least in my case). I started playing around with it, checked some online resources, and devised two solutions. The local solution works on my local machine with the XAMPP server installed on it and the online solution works on my Dreamhost account.

The Local Solution

RewriteEngine On
RewriteBase /ci/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /ci/index.php/$1 [L]

The only change you need to make is to "ci" (on lines 2 and 5) which is the folder where you have your CodeIgniter application installed. In brief, this rewrite file tells your web server to apply the rewrite rule whenever a file or a directory is not found on the server. For example, if you invoke URL "c", the "contact" folder is not found on your server (Since CodeIgniter files are in the "system" folder), and accordingly the URL is rewritten to "d". This rewrite allows CodeIgniter to execute successfully (By using URL "d") while giving you the benefits of shorter URLs (URL "c").

c) http://localhost/ci/contact
d) http://localhost/ci/index.php/contact

The Online Solution

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]

There are no changes that you need to make for this .htaccess file. However, there's one important note to mention. The question mark after "index.php" on line 5 is needed on my online hosting account at Dreamhost. You might want to remove it if it doesn't work on yours. Again, I didn't have the time to investigate why this is the case. Please check the additional resources for more details.

Conclusion

I hope this post saves you a few headaches I had to go through to solve this problem. I would love to see such a solution coming out of the box with the next version of CodeIgniter. If you have any hints or additional information regarding URL rewriting in the context of CodeIgniter, please share them in the comments.

Additional Resources

CodeIgniter URLs
http://ellislab.com/codeigniter/user-guide/general/urls.html

URL Rewriting for Beginners
http://www.addedbytes.com/apache/url-rewriting-for-beginners/

An easy way to test your RewriteRules against different URLs
http://civilolydnad.se/projects/rewriterule/

Dreamhost and CodeIgniter URLs
http://ellislab.com/forums/viewthread/55620/


Development SEO

Share this post


Written by
Mario Awad

Founder of SOFTKUBE, lead developer, and getting things done addict. Passionate about open source, user interface design, business development, and the tech world.

More about Mario Awad


About
SOFTKUBE

A small team of experts developing simple, usable, and high-quality web solutions. We blog about business, entrepreneurship, web development, and technology.

More about us


Recent Posts

Custom Theme Migration from Drupal 9 to Drupal 10

How to Change the Most Recent Git Commit Message

How to Make Google Chrome Forget a Permanent HTTP 301 Redirect

Finding and Fixing Unintended Body Overflow to Remove Horizontal Scrollbars

View all posts


All Posts Categories

Business Cheat Sheets CLI Design Development Downloads Drupal Email Google Apps HID Keyboards Multilingualism Open Source Philosophy PHP Pointing Devices Productivity Quotes Science Security SEO Technology Thoughts Windows Zend Framework