Common .htaccess for clean URI’s in CI

I remember when I started with CI back in the day that I always had a issue trying to get mod_rewrite to work so I could work with clean URLs in CI. Well it’s really not all that hard but I decided I will give you a very common .htaccess that should work on most web hosts. I will say that Godaddy is very abnormal in their structure so I’ve provided a alterative example for Godaddy.

First lets change a few things within your CI configs to make it work.
Open up config.php in application/config/config.php and edit this line:
‘$config['index_page'] = ‘index.php’;’ and replace it ‘index.php’ with a blank quotes ”.

Next depending on your server’s setup you may need to update this line:
‘$config['uri_protocol'] = ‘AUTO’;’
and change AUTO to QUERY_STRING or REQUEST_URI depending on your server setup. For example Godaddy uses QUERY_STRING.

Now in the root of your application create a file called ‘.htaccess’ and place the following code into it, if using Godaddy then choose the second example.

Common .htaccess

RewriteEngine On
RewriteCond $1 !^(index\.php|static)
RewriteRule ^(.*)$ index.php/$1 [L]

In this example you will want to include any folders/directories and files that you need accessible, separated by pipes ‘|’.

Godaddy (Query String) .htaccess

RewriteEngine On
RewriteBase /

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

 

Give this a go, I hope it will help you out with any issues you might come across trying to implement this.

Thanks

  • Tony

    The GoDaddy version is how I have set .htaccess for virtually every URI rewriting site I’ve worked on.

    The !-f and !-d switches just allow access to any real file and directories, but rewrite everything else through index.php. There is nothing GoDaddy specific there AFAIK.

    • LearnCodeIgniter

      Thanks for your comment, regardless if you specify directories or use the !-f/!-d switches, I found that the Godaddy method doesn’t work on all services, while it will work on most shared hosting servers. The part that is different there is index.php?$1 and index.php/$1.

  • Chrisyeung

    Is there any difference for Window shared 4GH Hosting and Linux shared 4GH Hosting service? I read from other threads that Windows server is more troublesome.

  • http://www.expertsfromindia.com/codeigniter-development.htm Cheryl Ray

    Thank