Skip to main content

Removing index.php from CodeIgniter installation

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.AllowOverride On

Second we need to check mod_rewrite is installed and enabled.

rewrite_module 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.

$config = ''

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

Popular posts from this blog

jQuery support in Aptana Studio 3

I use Aptana Studio primarily for web development. It has great support for languages like PHP and JavaScript. But the problem is that I mostly use jQuery rather than code in JavaScript and Aptana does not have good support / content assist from beginning. So we need to install ruble for jQuery library. Ruble is short for Ruby bundle and is a runtime environment that allows the extensibility of Aptana Studio's editors by using Ruby. To install the jQuery bundle follow the following steps: Navigate to Commands | Bundle Developement | Install Bundle. Select jQuery and click OK. Aptana will clone jQuery ruble from github. Include appropriate jQuery sdocml in Project Build Path from Project Properties. Include a VSDoc file for jQuery in your project. [caption id="attachment_47" align="aligncenter" width="1313"] Aptana fetching jquery ruble from GitHub[/caption] You must include appropriate version VSDoc for correct Content Assist. [caption id="attac...

How to split large mp3 files using mp3splt

If you have a  lengthy  or large MP3 file and you want to  split  it into files of smaller size or duration, You can easily do this using mp3splt. Mp3splt   is an utility to split mp3 and ogg vorbis digital audio files without the need of decoding. It can be used to split a file into no of tracks of fixed duration and size or specified  split points. It supports variable bit rate mp3 files, silent detection, splitting with local .XMCD, .CDDB or .CUE file splitpoints or from external servers like   tracktype.org . It copies the original files first and then generates new, smaller files during the process of splitting without altering the original file. It does not do any encoding or decoding so the splitting is very fast and lossless. Steps to follow Navigate to directory where you have downloaded mp3splt { if using portable version } . Run and execute mp3slpt using the command   mp3splt  . Specify the options according to which you wanna split. The basic syntax is   mp3splt [file path]...