4

How do I remove index.php from my URL in CodeIgniter?

I removed index.php from my configuration file, and I have run my rewrite_module in Apache (2.2.11) and my .htaccess file is:

RewriteEngine on

RewriteCond $1 !^(index\.php|images|captcha|css|js|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]

Now, whenever I click any link, it shows that the URL isn't found. What is the problem?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mehdy
  • 41
  • 1
  • 2

7 Answers7

4

Try this out in your .htaccess file: The comments should help get it tweaked in for you...

This works perfectly for me in several CodeIgniter website installations. Also, if mod_rewrite is not installed, it will send you to a 404 page (or you can change that to suite your purpose).

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /

    #'system' can be replaced if you have renamed your system folder.
    RewriteCond %{REQUEST_URI} ^system.*
    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

    #This last condition enables access to the images and css folders, and the robots.txt file
    RewriteCond $1 !^(index\.php|(.*)\.swf|images|robots\.txt|css|docs|cache)
    RewriteRule ^(.*)$ index.php/$1 [L]
    </IfModule>

<IfModule !mod_rewrite.c>
    # If we don't have mod_rewrite installed, all 404's
    # can be sent to index.php, and everything works as normal.
    ErrorDocument 404 /application/errors/404.php
</IfModule>

Check your virtual hosts file and see if you have these items set -- these may allow your .htaccess file to rewrite the URL correctly:

Options FollowSymLinks
AllowOverride All
Order deny,allow
Deny from all
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Shane
  • 16,779
  • 5
  • 27
  • 46
  • thank you shane , i have tried this before from http://codeigniter.com/wiki/mod_rewrite/">http://codeigniter.com/wiki/mod_rewrite/ but its not working . – Mehdy Feb 03 '10 at 14:22
  • I just extended the answer, please have a look and see if that helps. – Shane Feb 03 '10 at 14:34
  • thanks shane , i have set this in my http.conf file its looks like Options FollowSymLinks AllowOverride all Order deny,allow Deny from all but its not working – Mehdy Feb 03 '10 at 14:52
  • From what I can tell this should be working. My next suggestion would be for you to create a new virtual host, with a very simple structure (not even CodeIgniter probably) and test out some .htaccess rules to make sure it is even working correctly. If that works then install a fresh CI and get it working there. Too many variables and moving parts in your system may be making it hard to diagnose the issue.. Simple is always better when debugging. :) – Shane Feb 03 '10 at 15:07
3

This 100% works, I've tried it. Add only the following in your .htaccess file:

RewriteEngine On
RewriteCond $1 !^(index\.php|(.*)\.swf|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
iMagdy
  • 511
  • 4
  • 5
2

I also had a problem with the rewrite rule suggested in the CodeIgniter user guide. I solved it by removing the slash from in front of index.php

RewriteRule ^(.*)$ index.php/$1 [L]
Stephen Curran
  • 7,433
  • 2
  • 31
  • 22
0

First of all, make sure that you have defined a default controller. Then make sure that you construct URLs in such a way that there is a controller associated with them. For example,

Home page:

example.com/home/

Or for the About us page:

example.com/about/  

Also double check that you have turned on the mod-rewrite module.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sarfraz
  • 377,238
  • 77
  • 533
  • 578
  • thanks ahmed , yes i have defined my default controller my home page in route.php $route['default_controller'] = "welcome"; and my config.php $config['base_url'] = "http://localhost/codeigniter2/"; – Mehdy Feb 03 '10 at 13:43
0

I also had this problem and found that Shane's answer helped me out. I have my sites setup in WAMP.

RewriteBase /mysite/

The mysite is the alias as set in Apache. This worked for all my sites.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
0

.htacess

RewriteEngine on

#Any HTTP request other than those for index.php, 
#assets folder, files   folder and robots.txt
#is treated as a request for your index.php file.

RewriteCond $1 !^(index\.php|assets|files|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]

after this go to application/config/config.php and modify these two variables with the values mentioned below.

$config['base_url'] = '';

and

$config['index_page'] = '';

isnvi23h4
  • 1,910
  • 1
  • 27
  • 45
0

For all my Codeigniter projects, I use .htaccess code below:

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

I hope this helps.

lomse
  • 4,045
  • 6
  • 47
  • 68