CodeIgniter by default routes URLs through index.php. It is a bad practise from SEO perspective and also makes URLs ugly.
In order to remove index.php from CodeIgniter we need to create a .htaccess file in the root directory of CodeIgniter installation.
First of all we need ensure that AllowOverride is On in order to .htaccess to work.
Second we need to check mod_rewrite is installed and enabled.
In the .htaccess file created above we need to write following rules.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# PHP under mod_php5
<IfModule mod_php5.c>
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
# PHP under fast-CGI or other
<IfModule !mod_php5.c>
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
</IfModule>
Note the difference between mod_php5 and fast-CGI RewriteRule. fastCGI rule has a ‘?’ after index.php.
What Rewrite rule behind the scenes does is that if we call controller/action then it redirects to index.php/controller/action.
^(.*)$ is a regular expression that matches all the string passed.
RewriteCond adds exception for directories and other types of files.
Next from your application/config/config.php find variable $config['index_page'] and set it to blank string.
If CodeIgniter is installed in a sub-directory RewriteBase and RewriteRule will need to be changed accordingly. For example if CodeIgniter is installed in /public_html/CodeIgniter/ then RewriteBase and RewriteRule would be like following
RewriteBase /CodeIgniter/
RewriteRule ^(.*)$ /CodeIgniter/index.php/$1 [L]
If above configuration is still not working you’ll need to play around $config['uri_protocol'] in /application/config/config.php.
Final note—> I have installed html5boilerplate on my root directory which has some rewrite rules by default and I had installed CI in a sub-directory so I had to change my CI installation .htaccess to
RewriteBase /
RewriteRule ^(.*)$ sub-directory-name/index.php?/$1 [L]
Comments
Post a Comment