CodeIgniter is a widely used PHP framework that is helpful for developers to create robust web applications. One of the challenges that developers face while working with CodeIgniter is removing the index.php file from the URL. If you have deployed the CodeIgniter web application with the Apache web server, there is a quick and easy way to handle this. This can be done by creating the .htaccess file, which is a configuration file that allows developers to control the behavior of the Apache web server.
In this article, we will explain how to remove the index.php file from the URL in CodeIgniter by using the .htaccess file.
Prerequisites
Assuming that you have already installed an Apache server on your system. Also, you have already configured your Codeigniter application to work with Apache.
Enable Apache Rewrite Module
Before we begin, it’s important to note that the mod_rewrite module is not always enabled on the Apache web server. If it’s not enabled, you’ll need to enable it by the following methods:
- Redhat-based Systems: Edit the Apache configuration file (httpd.conf). To do this, open the httpd.conf file and locate the following line:
LoadModule rewrite_module modules/mod_rewrite.so
Remove the “#” symbol at the beginning of the line and save the file. This will enable the `mod_rewrite` module, which is required for using the .htaccess file.
- Debian-based Systems: All the Debian-based systems like, Ubuntu, Pop!_OS, and Linux Mint execute the following command to enable the mod_rewrite module.
sudo a2enmod rewrite
You may also need to check whether the .htaccess is enabled for your virtual host or not.
Configure .htaccess File
Once you’ve enabled the mod_rewrite module and the .htaccess for the virtual host, you can proceed with the next steps.
- Create a .htaccess file in the root directory of your CodeIgniter application.
- Open the .htaccess file and add the following code:
RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php/$1 [L]
This code tells the Apache web server to redirect all requests to the index.php file, except for requests to existing files and directories.
- Open the
config.php
file located in theapplication/config
directory and set the index_page variable to an empty string:$config['index_page'] = '';
- Finally, restart the Apache web server to apply the changes.
sudo systemctl restart apache2
# Debian-based systems sudo systemctl restart httpd
# Redhat-based systems
With these steps, you should now be able to remove the index.php file from the URL in your CodeIgniter application.
Conclusion
In conclusion, by following the steps above, you can remove the index.php file from the URL in your CodeIgniter application by using the .htaccess file. This can make your application’s URLs more user-friendly and improve the overall user experience. Keep in mind that it is a best practice to test the changes in a development environment before deploying to a production environment.