Codeigniter, a modern PHP framework with MVC approach comes with many inbuilt features including custom Routes
configuration. In every Codeigniter application there is index.php
in URL because all controllers passes through main index.php
which is inside route directory of an CI application.
Removing index.php
will make your application more SEO friendly.
We ca easily remove index.php
from URL with simple .htaccess
tweak. Let’s dive into the code and do this.
Step 1: Some changes in config file
First, we need to change some config variables from config.php
which is inside application/config/config.php
directory.
The default value of index_page
is index.php
but we need to change this
// default setting
$config['index_page'] = 'index.php';
Change above line into
$config['index_page'] = '';
In some cases the default setting for uri_protocol does not work properly.
// default uri_protocol value
$config['uri_protocol'] = 'AUTO';
Change above line into
$config['uri_protocol'] = 'REQUEST_URI';
Now we are done with the first step, so let’s move into second one.
Step 2: Writing or editing .htaccess
Second step is little tougher than first step. Now we need to create .htaccess
file inside root directory of an application. If you already have .htaccess
file just append these codes inside your file.
Create .htaccess
file with the following code or append these code into existing file
<ifmodule mod_rewrite.c>
RewriteEngine on
RewriteBase /myapplication/
RewriteCond $1 !^(index\.php|images|js|uploads|css|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
</ifmodule>
In this .htaccess
file myapplication
is your CI Application directory. If your application is hosted of root domain or root web server, you don’t need to write application directory name. Hence the syntax became like this.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond $1 !^(index\.php|images|js|uploads|css|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
</IfModule>
Conclusion
In this tutorial we easily made and Codeigniter application without index.php. Comment us for what kind of tutorials you want.
If you liked this article, then please subscribe to our YouTube Channel for useful videos. You can also find us on Twitter and Facebook.
Write a Reply or Comment