20

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.

Carson Myers
  • 37,678
  • 39
  • 126
  • 176

9 Answers9

18

Tried the answer here but that didn't work for me. So i tried the following and it did...

create the .htaccess in the root directory where the index.php along with license.txt is available. enter the following code in the file and store it:

DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|images|css|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]  

done! try it and it will surely work. Hope it helps

sys_debug
  • 3,883
  • 17
  • 67
  • 98
18

In your system/application/config/config.php, change

$config['index_page'] = "index.php";

to

$config['index_page'] = "";

and in your .htaccess, add this:

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

Add your .htaccess file to the same directory where the system folder is located, so in your CodeIgniter root directory, you should have

  • system/
  • user_guide/
  • index.php
  • .htaccess
  • license.txt

Also, since you have multiple sites running on your server, you might want to have VirtualHosts. Add this to the last part your apache2.conf for each site that you have:

Listen *:11000
<VirtualHost *:11000>
     ServerAdmin you@somewhere.com
     DocumentRoot "/var/www/cms"
     ServerName you-dummy.com
     <Directory "/var/www/cms">
          AllowOverride All
          Options +Indexes
          DirectoryIndex index.php
          Order allow,deny
          Allow from all
     </Directory>
     ErrorLog logs/cms_log
     CustomLog logs/cms_log common
</VirtualHost>

You may now access the site from http://localhost:11000/ and access the controller using http://localhost:11000/hello.

Randell
  • 6,112
  • 6
  • 45
  • 70
  • but the .htaccess is in `/var/www/cms/`, and I configured the base URL to be `http://localhost/cms`. In any case, neither of the URL's work – Carson Myers Sep 09 '09 at 01:42
  • this makes it refuse the connection, same with if I change the port to 80 too. I must be doing _something_ horribly wrong, I don't think it's normally this difficult to do this. – Carson Myers Sep 09 '09 at 02:05
  • Ubuntu linux. But you know what, instead of everything being in one "httpd.conf" file it's spread throughout a bunch. I figured since nothing at all was working I went digging through all the config includes and found where the directories were defined, and changed AllowOverride there. It doesn't work yet but I'm going to try everything again now that the .htaccess is actually getting opened. – Carson Myers Sep 09 '09 at 02:26
  • I've read your answer. Yeah, that `AllowOverride` set to `None` is not easy to catch. You only get to modify it almost only once for each installation so it's often forgotten. – Randell Sep 09 '09 at 03:02
  • 1
    +1 Randell your right. I had that the same problem that fixed it out. My thanks. – Fábio Antunes Jan 01 '10 at 23:19
  • Are you sure .htaccess should be added to the `system` folder? I thought it belongs in the root. – CyberJunkie Jul 18 '12 at 22:19
5

Had some problems with finding where to change the "AllowOverRide All".

From this guide I found the (default) file.

Remove Index.php CodeIgniter URL On Ubuntu

  1. Preliminary Note. I'm running all the steps in this tutorial with root privileges, so make sure you're logged in as root:

    sudo su
    
  2. Enable mod_rewrite module on apache. First enable the module as follows:

    a2enmod rewrite
    

    Change all occurrence of "AllowOverRide None" to "AllowOverRide All". Basically all "None" to "All" in the following file:

     gedit **/etc/apache2/sites-available/default**
    

    Restart Apache:

    /etc/init.d/apache2 restart
    
  3. Open file config.php on CodeIgniter (application\config). Remove index.php on $config['index_page'] = "index.php";

    $config['index_page'] = "";
    
  4. Make file .htaccess on CodeIgniter root directory. Fill the file with this code :

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

Now Index.php on CodeIgniter URL disappear

brasofilo
  • 25,496
  • 15
  • 91
  • 179
The Demz
  • 7,066
  • 5
  • 39
  • 43
2

This particular .htaccess file looks like it needs to go in your site's root (meaning, the currently executing site.)

Look into the documentation for RewriteCond to learn about what other kinds of conditions you can specify. Basically a RewriteRule will only be applied if all of its preceding RewriteConds are matched. You could try adding these rules to only redirect if the requested URL does not exist:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

mod_rewrite is very powerful, but it can be a real pain if it's not doing what you think it should. I don't know of any way to debug its directives, either.

As far as what else you can do with a .htaccess file, the Apache docs for htaccess files are always a good place to start. The most common use is for per-directory authentication. Most server configuration directives are supported in a .htaccess file (provided that the server's AllowOverride says so), but some have different syntax.

We Are All Monica
  • 13,000
  • 8
  • 46
  • 72
2

Try this one, it works perfetct

DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|images|css|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]  
Prashant
  • 21
  • 1
1

I had the same issues with CodeIgnitor links images,js,css. It all works now thanks to you guys! I have followed the below steps. consolidated all other comments into my scenario to work. Hope this helps somebody.

  1. changed httpd.conf

    <Directory --your root dir-- />
        AllowOverride All #was "none", but this was all commented out
        Order Deny,Allow
        Deny from all
    </Directory>
    
  2. created an empty .htaccess file in the htdocs\yoursite(site root) directory and pasted the following.

    DirectoryIndex index.php
    RewriteEngine on
    RewriteCond $1 !^(index\.php|images|css|js|robots\.txt|favicon\.ico)
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]
    
j0k
  • 22,600
  • 28
  • 79
  • 90
1

Put that at the document root of your Web site.

If you have multiple sites, put it at the top of each such site.

If you have multiple sites running from the same directory, you can use a RewriteCond to exclude or include particular sites to the rewrite rule.

You can exclude certain directories (eg /articles) the same way.

cletus
  • 616,129
  • 168
  • 910
  • 942
0
<IfModule rewrite_module>
RewriteEngine on
RewriteBase /

# Removes the "/" at the end
RewriteRule (.+)/$ /$1 [L,R]

RewriteCond $1 !^(index\.php|dhtml|img|css|js|favicon\.ico|robots\.txt)
RewriteRule ^([^?&]+) /index.php/$1 [L]
</IfModule>
w35l3y
  • 8,613
  • 3
  • 39
  • 51
0
RewriteEngine on
RewriteCond $1 !^(index\.php|images|css|js|robots\.txt|favicon\.ico)
RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]

The simple one in .htaccess on root folder! Works for me.

brasofilo
  • 25,496
  • 15
  • 91
  • 179
user2606817
  • 115
  • 2
  • 4