Friday, October 7, 2011

Remove index.php from url of codeigniter site using htaccess



Usually when we run a CodeIgniter website, the url will have index.php after the hostname. To remove this we need to use mode_rewrite, htaccess.

First just check your php info of your server whether you have loded mod_rewrite module. To check this create  'php-info.php' file and put the following contents and run it.

php-info.php

<?php
echo phpinfo();
?>

In your output, check whether in loaded modules 'mod_rewrite' is enabled or not.




If its enabled as given above, You are ready to use '.htaccess'. Otherwise you need to load the module in your server.

To remove 'index.php' from the url, create a '.htaccess' file ( no file name, just '.htaccess') in the root directory of your website where 'index.php' file of the codeigniter resides. And write the followin contents...

.htaccess


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

The above code is to rewrite the url 'http://localhost/example.com/<something>' to 'http://localhost/example.com/index.php/<something>' except for folders and files.

If your website url is in the root of the server like 'http://example.com/index.php/<something>'. To use this like 'http://example.com/<something>' , The htaccess code in the Line 4 should be like 'RewriteRule ^(.*)$ /index.php/$1'.

2 comments:

  1. I enjoyed over read your blog post. Your blog have nice information, I got good ideas from this amazing blog. I am always searching like this type blog post. I hope I will see again.... Feel free to visit this website.

    ReplyDelete
  2. Needed to compose you a very little word to thank you yet again regarding the nice suggestions you’ve contributed here.php training in chennai

    ReplyDelete