44

I have never used CodeIgniter before, let alone ANY php framework and I thought I would give it a try. Everything is going fine except I cannot seem to remove the index.php from the URL and still access my pages.

I have never used the MVC structure so I am learning as I go, so forgive me if I'm doing this wrong.

I am trying to access a view I created called 'about_page.php' by simply typing in localhost/ci/about but currently I can only access it by using localhost/ci/index.php/about

The controller for the page is: /application/controllers/about.php
The Model for the page is: /application/models/about_model.php
And the View for the page is: /application/views/about_page.php

I have searched for a solution to this issue, but haven't been able to find one. Here is where I have already searched:

CodeIgniter - removing index.php
Codeigniter - how to remove the index.php from url?
http://www.farinspace.com/codeigniter-htaccess-file/

CodeIgniter comes with a .htaccess file in the 'application' folder which contains only Allow Deny From All. So I created a new .htaccess file in the root directory, http://localhost/ci/.htaccess and added this code to it:

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

When the .htaccess file is in the root directory I get a 500 Internal Server Error. When I copy the exact same file into the applications folder the 500 error goes away, but I still cannot access the about page by using localhost/ci/about

I have already changed $config['index_page'] = 'index.php'; to $config['index_page'] = ''; AND I tried changing $config['uri_protocol'] = 'AUTO'; to $config['uri_protocol'] = 'REQUEST_URI'; but I am still getting the Internal Server Error.

I went into the httpd.conf file and uncommented the mod_rewrite.so module so I know mod_rewrite is active.

Does anyone have any ideas why this isn't working or how I can get this work? I know there are alot of questions on StackOverflow on this subject but I couldn't find one that answered my question.

Am I doing this right? Should I even be able to access the about page by visiting localhost/ci/about or do I have to create an 'about' directory in the 'application' directory?

Community
  • 1
  • 1
Ty Bailey
  • 2,392
  • 11
  • 46
  • 79

11 Answers11

89

There are 3 steps to remove index.php.

  1. Make below changes in application/config.php file

    $config['base_url'] = 'http://'.$_SERVER['SERVER_NAME'].'/Your Ci folder_name';
    $config['index_page'] = '';
    $config['uri_protocol'] = 'AUTO';
    
  2. Make .htaccess file in your root directory using below code

    RewriteEngine on
    RewriteCond $1 !^(index\.php|resources|robots\.txt)
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1 [L,QSA]
    
  3. Enable the rewrite engine (if not already enabled)

    i. First, initiate it with the following command:

    a2enmod rewrite
    

    ii. Edit the file /etc/apache2/sites-enabled/000-default

    Change all AllowOverride None to AllowOverride All.

    Note: In latest version you need to change in /etc/apache2/apache2.conf file

    iii. Restart your server with the following command:

    sudo /etc/init.d/apache2 restart
    
MrWhite
  • 43,179
  • 8
  • 60
  • 84
Rahul Chipad
  • 2,373
  • 17
  • 20
  • 13
    `change the AllowOverride None to AllowOverride All` This is so important but barely mentioned. Your .htaccess doesn't matter until this has been changed. – L-R May 11 '13 at 18:11
  • On Mac OS X, I changed this in /etc/apache2/users/username.conf and doing nothing more than using the .htaccess provided at http://ellislab.com/codeigniter/user-guide/general/urls.html – Dean Or May 01 '14 at 02:23
  • There are other answers here saying the same, change to $config['index_page'] = '' and $config['uri_protocol'] = 'AUTO' and change the htaccess, but all forget to mention AllowOverride all, if you don't do this, it won't work. (it work also with $config['uri_protocol']= 'REQUEST_URI' btw) – Gustavo Rodríguez Jun 09 '22 at 21:15
8

Your .htaccess is slightly off. Look at mine:

 RewriteEngine On
 RewriteBase /codeigniter  

  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond $1 !^(index\.php|images|robots\.txt|css|docs|js|system)
  RewriteRule ^(.*)$ /codeigniter/index.php?/$1 [L]

Notice "codeigniter" in two places.

after that, in your config:

base_url = "http://localhost/codeigniter"
index = ""

Change codeigniter to "ci" whereever appropriate

Venkata Krishna
  • 4,287
  • 6
  • 30
  • 53
jmadsen
  • 3,635
  • 2
  • 33
  • 49
  • @Rahul solution should be the perfect solution but didn't work somehow and your solution helped, using question mark in rewriterule after index.php – justnajm Jun 28 '16 at 08:51
  • 1
    `Notice "codeigniter" in two places.` - You don't _need_ "codeigniter" in two places in the example posted. In the above example the `RewriteBase` directive is superfluous. Although it would be preferable to remove the path from the `RewriteRule` _substitution_ and keep the `RewriteBase`. ie. `index.php?/$1 [L]` – MrWhite Oct 12 '16 at 21:11
  • Thank you. I have tried so many htaccess code. This is the one which is working fine for me – sujivasagam Sep 15 '17 at 05:47
4
<IfModule mod_rewrite.c>
   RewriteEngine On
   RewriteBase /dmizone_bkp
   RewriteCond %{REQUEST_FILENAME} !-f
   RewriteCond %{REQUEST_FILENAME} !-d
   RewriteCond $1 !^(index\.php|images|robots\.txt|css|docs|js|system)
   RewriteRule ^(.*)$ /dmizone_bkp/index.php?/$1 [L]
</IfModule> 
Narendra
  • 3,069
  • 7
  • 30
  • 51
Vinilnbr
  • 41
  • 2
  • If you are using a `RewriteBase` directive then you don't need to include the path in the `RewriteRule` _substitution_ as well. ie. `index.php?/$1 [L]` would be sufficient (and preferable). – MrWhite Oct 12 '16 at 21:07
  • What does last `RewriteCond` mean? `RewriteCond %{REQUEST_FILENAME} !-f` isn't sufficient? – max Feb 07 '18 at 20:00
3

This works for me

Move your .htaccess file to root folder (locate before application folder in my case)

RewriteEngine on
RewriteBase /yourProjectFolder
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]

Mine config.php looks like this (application/config/config.php)

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

Let me know if its working for you guys too ! Cheers !

William Kheng
  • 671
  • 11
  • 15
  • Or, move the `.htaccess` into the `/yourProjectFolder` (I think `/ci` in the OPs case) and remove the `RewriteBase` directive altogether. (Incidentally, you don't need the `PT` flag in per-directory `.htaccess` files - it is implied.) – MrWhite Oct 12 '16 at 21:04
2

I am using something like this - codeigniter-htaccess-file, its a good article to begin with.

  • leave the .htaccess file in CI root dir
  • make sure that mod_rewrite is on
  • check for typos (ie. controller file/class name)
  • in /application/config/config.php set $config['index_page'] = "";
  • in /application/config/routes.php set your default controller $route['default_controller']="home";

If you are running clean installation of CI (2.1.3) there isn't really much that could be wrong.

  • 2 config files
  • controller
  • .htaccess
  • mod_rewrite

read

Community
  • 1
  • 1
Velja Matic
  • 560
  • 4
  • 15
  • Claim that not many things can go wrong is generally true on Windows. On Linux things may go wrong for other reasons (same may apply to some versions of Windows server). In essence IMHO this possibly most complete answer on where to look for issues within CodeIgniter. (only exception to completeness is that PHP may be run in other environments - other web servers, but from the question looks like that Apache is the environment) – ljgww Jun 23 '18 at 15:12
2

For those users of wamp server, follow the first 2 steps of @Raul Chipad's solution then:

  1. Click the wamp icon (normally in green), go to "Apache" > "Apache modules" > "rewrite_module". The "rewrite_module" should be ticked! And then it's the way to go!
MrWhite
  • 43,179
  • 8
  • 60
  • 84
Joshua Kusaasira
  • 171
  • 1
  • 10
  • This worked. I had all the configurations correct but until I did this, nothing worked. Thanks. – Rhokai Jan 12 '17 at 13:04
1

if not working

$config['uri_protocol'] = 'AUTO';

change it to

$config['uri_protocol'] = 'PATH_INFO';

if not working change it to

$config['uri_protocol'] = 'QUERY_STRING';

if use this

$config['uri_protocol'] = 'ORIG_PATH_INFO';

with redirect or header location to url not in htaccess will not work you must add the url in htaccess to work

1

I had similar issue on Linux Ubuntu 14.

After constant 403 error messages in the browser and reading all answers here I could not figure out what was wrong.

Then I have reached for apache's error log, and finally got a hint that was a bit unexpected but perfectly logical.

the codeIgniter main folder had no X permission on folder rendering everything else unreadable by web server.

chmod ugo+x yourCodeIgniterFolder

fixed first problem. Killed 403 errors.

But then I started getting error 500.

Culprit was a wrong settings line in .htaccess

Then I started getting php errors.

Culprit was same problem as main folder no X flag for 'others' permission for inner folders in application and system folders.

My two cents for this question: READ Apache Error.log.

But, also be aware of security issues. CodeIgniter suggests moving application and system folder out of web space therefore changing permissions shall be taken with a great care if these folders remain in web space.

Another thing worth mentioning here is that I have unzipped downloaded CodeIgniter archive directly to the web space. Folder permissions are likely created straight from the archive. As given they are secure, but folder access issue (on unix like systems) is not mentioned in CodeIgniter manual https://codeigniter.com/user_guide/installation/index.html and perhaps it shall be mentioned there with guidelines on how to relax security for CodeIgniter to work while keeping security tight for the production system (some security hints are already there in the manual as mentioned beforehand).

ljgww
  • 83
  • 9
0

Just add this in the .htaccess file:

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]
Pang
  • 9,564
  • 146
  • 81
  • 122
Siddharth Shukla
  • 1,063
  • 15
  • 14
0

By default the below sits in the application folder, move it like suggested into the root dir.

leave the .htaccess file in CI root dir

Cyber
  • 2,194
  • 4
  • 22
  • 41
0

Open the application/config/config.php file and make the changes given below,

  1. set your base url by replacing the value of $config['base_url'], as $config['base_url'] = 'http://localhost/YOUR_PROJECT_DIR_NAME';

  2. make the $config['index_page'] configuration to empty as $config['index_page'] = '';

Create new .htaccess file in project root folder and use the given settings, RewriteEngine on RewriteCond $1 !^(index\.php|resources|assets|images|js|css|uploads|favicon.png|favicon.ico|robots\.txt) RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php/$1 [L,QSA]

Restart the server, open the project and you'll be good to go.

Kiran Maniya
  • 8,453
  • 9
  • 58
  • 81