Saturday, 22 October 2016

Remove index.php from url in codeigniter

Step 1. Create a file named " .htaccess " in parent directory of the codeigniter application. (remember the dot before the name of file)

Step 2. Copy and paste the following codes in the file and save.

<IfModule mod_rewrite.c>
RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>


Step 3. Check the url of your controller without "index.php". 

If it does not work, read the following instrunctions for further configuration. 

The .htaccess,  you just created, rewrite the url by removing index.php and these lines are defined in regular expression. But for the .htaccess file to work, Apache server( or any of your web server) needs certain configuration. 

To allow apache to use .htaccess file you will have to " Allow Rewrite rule" and if you want to do that follow these instructions step by step.

Step 1. First of all don't mess with the file you created earlier name .htaccess and leave that file as it is. 

Step 2. Go to your apache root folder and open apache2.conf( In linux) and httpd.conf(in windows)

For linux the path is /etc/apache2/apache2.conf

For windows the path is c:/wamp/bin/apache/httpd.conf 
 (you can also directly open the file from windows task bar) 


Step 3. Change inside <Directory /var/www> tag
  AllowOverride None to AllowOverride All



 

Step 4. Save the file and exit. 

Step 5. Open terminal and type commad a2enmod rewrite and your are done.

Congratulations !

Now you apache server is configured to allow .htaccess file to rewrite your URL.


For summer training and workshops 

Visit http://www.cybercreeksolutions.com/