31

I am having trouble removing index.php from my URLs in Codeigniter. I've made a few websites with Codeigniter 1.7 and the .htaccess code I used doesn't work in 2.

I have tried using

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]

RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /index.php?/$1 [L]

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

and

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt|css)
RewriteRule ^(.*)$ ./index.php/$1 [L]

</IfModule>

I've also tried it without RewriteBase / in.

I have changed the $config['uri_protocol'] to REQUEST_URI and QUERY_STRING and nothing.

I have set $config['index_page'] = "";

The file structure is 192.168.0.130/(site)/ so it must be going back to the root of the server and can't find the index.php file.

All of the controllers I have made can be reached by putting 192.168.0.130/(site)/index.php/testcontroller

Thank you.

Apologies if this has been asked before - I have looked and tried what I could see.

Edit:

I should also add that I changed the default folders to be

application

CI-2.0

index.php

and changed the paths in index.php to be correct.

Paul
  • 1,122
  • 1
  • 10
  • 18
  • Do you have any htaccess files in the parent folder? – timwebdev Mar 01 '11 at 13:52
  • There is a htaccess file. It only has one line in it and it's for a different project - I just tried commenting it out but it didn't do anything. – Paul Mar 01 '11 at 13:54
  • Btw: The system and application RewriteConds have to start with /SUBFOLDER/ – timwebdev Mar 01 '11 at 13:55
  • I tried adding the subfolder but still nothing. I've also edited the post to say that I changed the default folder name for system to CI-2.0. – Paul Mar 01 '11 at 14:02
  • `192.168.0.130/(site)` is (site) `root`/contains your index.php? else you will need to add the directory to `Rewrite Base`. – Ross Mar 01 '11 at 14:23
  • I have tried adding both /192.168.0.130/site/ and /site/ for Rewritebase and I still get the error. – Paul Mar 01 '11 at 14:32
  • if server is windows use webconfig file instead of htacces – Visakh B Sujathan May 30 '15 at 12:01

7 Answers7

24

Try the first code block you posted, but instead of /index.php try using /(site)/index.php (obv replacing (site) with whatever your site folder is named).

WNRosenberg
  • 1,862
  • 5
  • 22
  • 31
  • I just tried it and it didn't work. This is going to turn out to be something stupid I've missed... Thank you for the help. – Paul Mar 01 '11 at 15:01
  • I just moved it to my local server and it works with these .htaccess settings so it must be a server problem. – Paul Mar 01 '11 at 15:09
  • 2
    This might sound like a dumb question, but did the server you were originally testing on have mod_rewrite enabled? – WNRosenberg Mar 01 '11 at 15:25
  • I just found why this was not working with my local EasyPHP setup... If you are using an alias for your website, you will need to change "RewriteBase /" to "RewriteBase /alias". – guiweb Oct 15 '14 at 20:32
  • Guys take care of the "RewriteBase"! Put in your subfolder where CodeIgniter is located. – s1x Oct 19 '14 at 18:29
15

This worked for me:

  1. Create your htaccess file
  2. Set $config[‘index_page’] to an empty (config.php)
  3. string Set $config['uri_protocol'] = 'REQUEST_URI'; (config.php)

This is my htaccess file:

Options -Indexes
Options +FollowSymLinks

RewriteEngine On
RewriteBase /

#Removes access to the system folder by users.
#Additionally this will allow you to create a System.php controller,

RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ index.php?/$1 [L]

#When your application folder isn't in the system folder

RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /index.php?/$1 [L]

#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn't true it sends the
#request to index.php

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

# If we don't have mod_rewrite installed, all 404's
# can be sent to index.php, and everything works as normal.
# Submitted by: ElliotHaughin   

ErrorDocument 404 /index.php

Source:

http://taggedzi.com/articles/display/codeigniter-2-htaccess-and-friendly-urls

Robin Morgan
  • 181
  • 2
  • Thank you. I did actually get it working fine with the same config I had on the local server and the production server so it must have been issue with the dev server we have. – Paul Mar 12 '11 at 13:54
  • Sorry guys, I tried every htaccess code, I dig the Google 5 days and tried every htaccess code sample but every one fail. But, finally this code is working for me after adding 'RewriteBase /my/base/site/url' in .htaccess code. Before add above line, when I am try to call my CI controller it was being redirect on localhost wamp page. So, I added above code as 'RewriteBase' //config.php $config['base_url'] = ''; $config['index_page'] = ''; $config['uri_protocol'] = 'REQUEST_URI'; he he.. :) Coooollll!!!!! Robin Morgan and Thank you so much.. :) – Nono Mar 01 '13 at 07:46
3
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.*)$ index.php?/$1 [L]

This much is only required in root folder add this to .htaccess

Shaiful Islam
  • 7,034
  • 12
  • 38
  • 58
saurabh kamble
  • 1,510
  • 2
  • 25
  • 42
  • this has been the only htaccess that has worked on both my local (xamp on wamp) and hosted server (lamp) – dsdsdsdsd May 26 '16 at 03:52
3

I was on this for 2 days.. Tried everything possible. I've just came out that you need the following:

TYPE:

sudo nano /etc/apache2/sites-available/default

And change AllowOverride None to AllowOverride All - This will give access to your .htaccess files

<Directory /var/www/>
            Options Indexes FollowSymLinks MultiViews
            AllowOverride All
            Order allow,deny
            allow from all
            # Uncomment this directive is you want to see apache2's
            # default start page (in /apache2-default) when you go to /
            #RedirectMatch ^/$ /apache2-default/
</Directory>

And also enable mod rewrite on Apache:

http://stackoverflow.com/questions/3131236/how-do-you-enable-mod-rewrite

Hope this helps, Mário

Mário Carvalho
  • 3,106
  • 4
  • 22
  • 26
2

After following all instructions from other people and from this CodeIgnitor URL documnetation, don't forget to restart the server. I simply forgot to do that and kept trying all other solutions.

Jeromy French
  • 11,812
  • 19
  • 76
  • 129
neelabh
  • 479
  • 6
  • 19
  • note: some web hosts do not require you to restart the server after updating a .htaccess file. For example, hostgator.com does not require a restart: http://support.hostgator.com/articles/specialized-help/technical/apache-htaccess/must-i-restart-apache-after-changing-a-htaccess-file – Alex Spencer Dec 02 '12 at 06:43
1

You just need to add below code in .htacess.

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

1) Create .htaccess file in your root folder

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

2) Edit application/config/config.php file in Application folder

$config['base_url'] = 'http://localhost/projectName';

 $config['index_page'] = '';         // remove index.php 

i hope it will work for you

tushar
  • 161
  • 1
  • 2