Introduction
This tutorial shows how to remove index.php from URL in Codeigniter. You may want to remove the index.php file so that clean URL looks good and it makes better readability. Also the clean URL is easy to remember.
For removing index.php from the URL you need to have .htaccess
file under the project’s root directory and mod_rewrite
enabled for your Apache HTTP Server.
1. Create .htaccess file
A directory-based configuration file for Apache web servers is called .htaccess
file.
It controls the directory by changing URL rewriting rules, rerouting requests, and much more methods,. It should therefore be utilized to ensure that our website's security is maintained regardless of future changes to the code or design.
Create a .htaccess file in the root of your codeigniter project and enter the following code :
RewriteEngine On
RewriteBase /your_project_folder_name
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php/$1 [QSA,L]
WHERE :
your_project_folder_name
: If you are working locally , change this with the name of your project folder. If you are working online , dont add add anything . Keep the RewriteBase
as "/".
2. Update.config.php file :
- Open "config.php
" file located in application/config/
folder.
- Search for "$config['index_page'] = "index.php";
"
- Remove the "index.php
"
==> Finally, you will get : $config['index_page'] = "";
- Then seach for : $config['uri_protocol'] = "AUTO";
and replace it with : $config['uri_protocol'] = "REQUEST_URI";