CodeIgniter | Remove index.php from URL

CodeIgniter | Remove index.php from URL

Hi, we’ll see how to remove index.php from codeigniter url. Even though codeigniter uses search engine friendly urls, nobody would have missed the awkward presence of'index.php' in between those codeigniter's urls. Have you ever wondered how to get rid of it to get more clean urls in your codeigniter application? Say your app has an url something like http://www.example.com/index.php/contact and you want it to look like http://www.example.com/contact. Yes, you can do it. In codeigniter removing index.php from url should be done by rewriting urls in htaccess file.
codeigniter-remove-index.php-from-url

Step-by-Step Instructions to Remove index.php in CodeIgniter URL

Step 1: Enable Mod Rewrite Option in your APACHE Server

To rewrite urls in the htaccess file, the mod_rewrite option should be enabled in the apache server. Goto apache's "httpd.conf" file and search for the line,
LoadModule rewrite_module modules/mod_rewrite.so
If the above line is preceded with # (in which case, the module is disabled), then remove the # (hash) symbol to enable url rewriting.

Step 2: Create '.htaccess' File

Next create the .htaccess file at the root directory. To create the htaccess file, open your favourite text editor and create a new text file. Save it as ".htaccess" (make sure you type the filename within quotes to avoid the file to be saved as text file) in the root directory.
htaccess-file-codeigniter
Now copy paste this code to the htaccess file and save it.
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
    RewriteCond %{REQUEST_URI} !/system/.* [NC]
    RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>
If you run your codeigniter app as a subdirectory instead of root domain like, http://example.com/cisite/ instead of http://example.com/ then the above code won't work. You have to tweak it little to tell that your site runs as a subdirectory. Add this code to the htaccess file instead of the above one.
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
    RewriteCond %{REQUEST_URI} !/system/.* [NC]
    RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>

Step 3: Modify CodeIgniter Config Settings

Open config.php file under application >> config folder and search for the line,
$config['index_page'] = 'index.php';
Remove index.php from it to look like this,
$config['index_page'] = '';
Now restart apache and check the url. eg., http://www.example.com/contactinstead of http://www.example.com/index.php/contact
If it doesn't work, you may be having problem with uri protocol. In that case find $config['uri_protocol'] ='AUTO' setting in config file and replace with $config['uri_protocol'] = 'REQUEST_URI'.
Restart apache server again and you can check the codeigniter site to see that index.php has been removed from codeigniter url.

No comments:

Post a Comment