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