Jun 13, 2008

HOWTO: Codeigniter and mod_rewrite

Author: gaweee | Filed under: development, howto

I cant remember the last time i got mod_rewrite to just work. I think i know regex, i think i know how http request works, i think i know some mod_rewrite. I try. For sure it breaks. This is undeniable proof that good things ONLY come to those who sweat and strain their necks…

The following is my mod_rewrite ruleset for codeigniter

Options +FollowSymlinks
RewriteEngine on
 
RewriteCond %{REQUEST_URI} (index\.php|webroot/|images/|css/|js/|robots\.txt|favicon\.ico)
RewriteRule ^(images|css|files|js)/(.*)$ webroot/$1/$2 [L]
RewriteRule ^(favicon\.ico)$ webroot/$1 [L]
 
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

I threw all my static files into webroot folder so that you can access the site via http://t.wits.g/images/myimage.jpg easily. Same goes for css and js.
So now you can access your controllers through the preferred pretty url: http://t.wits.sg/welcome
I’ll go hack the router.php file someday when i’m free enough so that it can accept GET requests too.


One Response to “HOWTO: Codeigniter and mod_rewrite”

  1. Jen Says:

    Hi there,
    I am hoping you can help me. I’ve scrubbed the net looking for a solution to why I can’t get my local development copy of CI to work correctly. Sure it has something to do with mod_rewrite function since my url’s work if I manually add back in /index.php/.

    For example, clicking a link gives me URL:
    http://localhost/ScenicDVL/products/jungle-food

    which results in a 404:
    Not Found

    The requested URL /ScenicDVL/products/jungle-food was not found on this server.

    If I change the URL to:
    http://localhost/ScenicDVL/index.php/products/jungle-food

    the page populates. doh!

    I am running WAMPSERVER, APACHE 2.2.3

    My current htaccess file reads:
    RewriteEngine on

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

    # if URL starts with /TinyMCE, stop processing here
    RewriteCond %{REQUEST_URI} ^/tinymce [NC]
    RewriteRule .* – [L]x

    I’ve added the following to the httpd.conf file (based on CI threads and other searches):

    RewriteEngine On
    RewriteCond %{REQUEST_URI} !^(/index\.php|/img|/js|/css|/robots\.txt|/favicon\.ico)
    RewriteRule ^(.*)$ /index.php/$1 [L]

    Options Indexes Includes FollowSymLinks MultiViews
    AllowOverride AuthConfig FileInfo
    Order allow,deny
    Allow from all
    :

    I’ve updated config base url’s to include the /index.php/ and that doesn’t even seem to register.
    I am a total newbie but think I’ve done my best trying to figure this thing out.

    HELP!

Leave a Reply