Search code examples
phpurl-rewritingcodeigniter-2

Rewrite URL not working in CodeIgniter


I have learned and make website using CodeIgniter. I want to create this system module base so I have created multiple folders and controller,views and model for each module and it's working fine. I want to remove index.php and hide query string like pass user id in URL. I have tried htaccess code which given user guide but it did not work.

Current URL :

mysite/sitename/index.php/departments/departments/delete_department/14 

Require :

mysite/sitename/departments/departments

I have tried below code in htaccess but it does not work as expected, it displays page not found error.

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

My directory structure is something like that

My site
    |_application
    |   |_ Controller
        |       |_Login (folder) // module folder
        |_Models
        |       |_pages 
        |
        |_views
            |_login (folder) // module folder

Solution

  • 1) you should have Rewrite mod on apache

    2) You should change application/config/config.php

    $config['index_page'] = 'index.php'; to: $config['index_page'] = '';
    

    3) after that I see that you are having a subdirectory of sitename so you should add that on your .htaccess as your RewriteBase which will result in an .htaccess like this:

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