Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Codeigniter - how to remove the index.php from url?

I have following structure of my project

/system
/applications
  /cache
  /core
  /helpers
  /hook
  /language
  /libraries
  /logs
  /third_party
  /admin-panel
     /config
     /controllers
        welcome.php
        dashboard.php
     /errors
     /models
     /views
        welcome.php
        dashboard.php
  /user-panel
     /config
     /controllers
            welcome.php
            dashboard.php
     /errors
     /models
     /views
        welcome.php
        dashboard.php
/admin
  index.php
index.php

My index.php inside my project folder has user-panel as my application folder and index.php in admin folder has admin-panel as my application folder.

I have inserted this line $route['default_controller'] = "welcome"; $route['welcome'] = 'welcome'; in routes.php inside both user-panel and admin-panel

I can access welcome controller of my both user-panel and admin-panel by using http://localhost/myproject and http://localhost/myproject/admin

but i cannot access my dashboard controller by using http://localhost/myproject/dashboard or http://localhost/myproject/admin/dashboard

my dashboard controller is accessible by using http://localhost/myproject/index.php/dashboard or http://localhost/myproject/admin/index.php/dashboard

But i dont want index.php included in my url. I want to remove it. I have also tried by using .htaccess inside my admin folder. I write following line in this

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

but this didn't work for me. It give 404 not found CI error. I have also enabled mod_rewrite by changing AllowOerride None to AllowOverride All in httpd.conf. Please tell me what should i do to remove index.php and where should i place my .htaccess files in my project directory.

like image 399
Naveen Chauhan Avatar asked Aug 14 '12 06:08

Naveen Chauhan


People also ask

How do I get rid of index php?

To remove the “index. php” from your site's URLs, you will first need to make sure your server is set up to pass would-be 404 requests off to Craft's index. php file behind the scenes. If you're running Apache, you can do that by creating a redirect in your site's .

Why is index php in my URL?

The first reason why index. php appears in the URL might be because the structure of permalinks is not set properly in WordPress Settings. So to verify if the structure of permalinks is set properly, let's check the permalink tab in WordPress Dashboard.


3 Answers

Be sure of the following :

  1. .htaccess needs to be placed in the same directory as the index.php file, usually root of the application.
  2. read the manual about codeigniter and .htaccess
  3. Be sure that apache is running the rewrite module named : rewrite_module you can read more about enabling the module on linux here : Blog post or for wamp here : Wamp icon -> Apache -> Apache Modules -> rewrite_module
like image 44
DonSeba Avatar answered Oct 11 '22 05:10

DonSeba


if your root folder for project is not root of domain i.e. your website is subdirectory of a domain http://localhost/myproject then you need an additional line in your .htaccess file that is RewriteBase

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

Also make sure, your config.php is configured as

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

And mod_rewrite is enable in apache's config file.

like image 176
atastrophic Avatar answered Oct 11 '22 03:10

atastrophic


Open config.php from system/application/config directory

and replace

$config['index_page'] = “index.php” by $config['index_page'] = “”

Create a “.htaccess” file in the root of CodeIgniter directory

and add the following lines.

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

In some case the default setting for uri_protocol does not work properly.

To solve this problem just replace

`$config['uri_protocol'] = “AUTO”` 

by

 $config['uri_protocol'] = “REQUEST_URI” 

from system/application/config/config.php

like image 21
rajmohan Avatar answered Oct 11 '22 05:10

rajmohan