title: How do I write a .htaccess file to make CodeIgniters URL routing work?
body: <p>I'm running a LAMP environment with CodeIgniter. I want to be able to use its URL pattern, like, <code>http://localhost/controller/function/ID</code>, but by default it has to be <code>http://localhost/index.php/controller/function/ID</code>. The user guide says that I need a .htaccess file to make the former style possible, and uses this as an example:</p>
  
  <pre><code>RewriteEngine on
  RewriteCond $1 !^(index\.php|images|robots\.txt)
  RewriteRule ^(.*)$ /index.php/$1 [L]
  </code></pre>
  
  <p>But I don't understand how this works. Where should I put the .htaccess? Does it have to be in the server root?</p>
  
  <p>What if I have multiple sites running on my server. Do I have to put it in <code>/var/www/</code> and specify conditions based on what directories are loaded? Or can I just put it in <code>/var/www/site_using_codeigniter/</code>? I'm not sure how to even get this to work.</p>
  
  <p>Can I make it so that it will only map to an MVC URL if the requested URL doesn't actually exist? For example, <code>http://localhost/articles/2009/some-article-name</code> wouldn't be a directory on the server, so it would map to <code>index.php</code> and be given to codeigniter, but <code>http://localhost/forum</code> <em>would</em> exist on the server but shouldn't be handled by codeigniter.</p>
  
  <p>Any other things I should do with a .htaccess file? Anything good to read about them?</p>
  
  <h2>update</h2>
  
  <p>I changed my config file like this:</p>
  
  <pre><code>$config['index_page'] = ""; //used to be "index.php"
  </code></pre>
  
  <p>and added this to a <code>.htaccess</code> file:</p>
  
  <pre><code>RewriteEngine on
  RewriteCond $1 !^(index\.php|images|robots\.txt)
  RewriteRule ^(.*)$ /index.php/$1 [L]
  </code></pre>
  
  <p>and saved it in my sites root directory (in this case <code>/var/www/cms/</code>)</p>
  
  <p>I went into /etc/apache2/conf.d/security (which is included by /etc/apache2/apache2.conf) and did this:</p>
  
  <pre><code>&lt;Directory /&gt;
      AllowOverride All #was "none", but this was all commented out
      Order Deny,Allow
      Deny from all
  &lt;/Directory&gt;
  </code></pre>
  
  <p>It's still not working however. If I navigate to <code>http://localhost/cms/index.php/hello</code>, it prints "Hello there," but <code>http://localhost/cms/hello</code> gives a 404 error.</p>
  
  <p>Ideas?</p>
  
  <h2>update: græt success!</h2>
  
  <p>I had to dig through my apache configuration files to finally find where all the directories were defined (I had never messed around in server config files before this date) and found that pesky <code>AllowOverride None</code> that was foiling my plans for greatness.</p>
  
  <p>Now it works perfectly. All of the answers here are viable and work.</p>