Codeigniter - controllers in subfolder, remove index.php from url

2.5k Views Asked by At

How can I remove 'index.php' from urls, if I have some controllers in the controllers folder and one in subfolder? For example my frontend url looks like this : domain.com/site/contact.html I would like my backend url look like this: domain.com/system/settings/profile.html, where system is not a controller, only a subfolder in the controllers folder. When I type domain.com/index.php/system/settings/profile.html, everything works fine, it just does not look right. Here's what's in my routes.php file:

$route['default_controller'] = "site";
$route['system'] = "system/login";
$route['404_override'] = 'errors/error_404';

Here's what's in my .htaccess file:

RewriteEngine on
RewriteCond $1    !^(index\.php|img|css|public|tmp|download|javascript|rte|document|xajax_js|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]

Thanks.

4

There are 4 best solutions below

1
On

You can try set RewriteBase like RewriteBase / or RewriteBase /site/

0
On

for removing index.php

create a .htaccess file in your root folder of project and write this much of code:

<IfModule mod_rewrite.c>

RewriteEngine On

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

</IfModule>

and save it.

remove the index.php from your URl and enjoy. please note that.htacess doesn't run on local machine.

0
On

My problem was a reserved controller name in routes.php file:

$route['system'] = "system/login";

You cannot name your subfolder 'system', because there is a folder 'system' in the root folder.

0
On

To remove index.php from URL first check mod_rewrite is enabled in apache after that add this in .htaccess file

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

in the .htaccess file and move the .htaccess file near to index.php location

/var/www/CodeIgniter/index.php
/var/www/CodeIgniter/.htaccess

in config.php in config folder set

$config['index_page'] = '';