remove index.php from codeigniter

How to Remove index.php from URL in CodeIgniter

By default, index.php is included in every URL in websites built using CodeIgniter PHP framework. It is tedious to include index.php in every requested URL of your visitors. In this article, we will look at how to remove index.php from URL in CodeIgniter.


How to Remove index.php from URL in CodeIgniter

CodeIgniter is a popular PHP framework for developing websites & applications. Here are the steps to remove index.php which is automatically added in every URL on your website, by default.


1. Enable mod_rewrite

We will be redirecting all URLs that contain index.php to ones that do not contain index.php. For this purpose, you need mod_rewrite Apache module. If you have enabled mod_rewrite in Apache, you can skip this step.

If you have enabled mod_rewrite on your Apache server, you can skip this step. Else open terminal and run the following commands to enable mod_rewrite in Ubuntu/Debian systems.

$ sudo a2enmod rewrite

It takes just one command to enable mod_rewrite in Ubuntu/Debian systems.

For CentOS/RHEL/Fedora systems, open Apache configuration file

$ sudo vi /etc/apache2/httpd.conf

Look for the following line

#LoadModule rewrite_module modules/mod_rewrite.so

Uncomment it by removing # at its beginning

LoadModule rewrite_module modules/mod_rewrite.so

If you don’t find the above line, just add it to the configuration file.

Look for the following section and change AllowOverride directive from None to All.

. . .
<Directory /var/www/html>
. . .
 # 
 # AllowOverride controls what directives may be placed in .htaccess files.
 # It can be "All", "None", or any combination of the keywords:
 # Options FileInfo AuthConfig Limit
 #
 AllowOverride All
. . .
</Directory>
. . .

Also read : How to Check What User Apache is Running As


2. Remove index.php from URL

Create .htaccess file at /var/www/html.

$ sudo vi /var/www/html/.htaccess

Please note that you need to add a dot(.) in .htaccess file name.

Add the following lines to your .htaccess file.

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

Save and close the file. Remove index.php file from /var/www/html.

In the above configuration %{REQUEST_FILENAME} has the URL part after your domain name. The next three lines check if the requested URL is not a regular file (-f) and not a directory (-d) then do a rewrite of the URL excluding index.php from it.

Also read : How to Change Default Page in .htaccess


3. Restart Apache Server

Restart Apache web server to apply changes.

$ sudo systemctl restart httpd

Open browser, and go to your website http://your-site-domain.com/index.php and it will automatically redirect you to http://your-site-domain.com.

That’s it. Now you don’t need to include index.php in requested URLs to your website. Also, if anyone includes index.php in the requested URL it will be automatically removed. In this article, we have learnt how to remove index.php from URL. You can use the above steps in other Apache/PHP based frameworks like Magento, Drupal, Joomla, etc.

Also read : How to Make URL Case Insensitive in Apache


Leave a Reply

Your email address will not be published. Required fields are marked *