C

Simplified tutorials to learn the popular basic language C

VB.Net

Learn the basic desktop development application language

CodeIgniter

One of the popular PHP three tier (MVC) framework

Removing index.php from URL Using htaccess


How do we run a CodeIgniter project? It’s very easy. But there is text ‘index.php’ on your CodeIgniter running url.




For example:

Here I am running a controller function ‘register’, where controller name is ‘user’ and the project name is ‘myproject’.

<?php
class User extends CI_Controller
 {
   public function register()
   {    

   }
}

Then the running url will be:

http://localhost/myproject/index.php/user/register

Note that, there is a text ‘index.php’ in link url. Have you ever noticed such a link contains 'index.php' on any PHP website links? Normally developers remove that ‘index.php’ from URL using some techniques.

Here let us see, how to do it…

There is a concept of ‘.htaccess’. Which is a file actually used to improve security, accessibility of your CodeIgniter project.

In your CodeIgniter folder, there should be a file named ‘.htaccess’ on your application folder. Just copy it into previous folder, that is your main project folder.

Removing index.php using htaccess in codeIgniter
  • Now open the using Notepad editor.
  • Remove all the content and copy and paste these line of codes on your .htaccess file.
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /myproject/index.php/$1 [L]

Where ‘myproject’ is your project folder name.

htaccess setup to remove index.php in codeIgniter url
  • Save your file (Ctrl + S).
Now all your project links works well without specifying ‘index.php’ on url.

For example:

Instead of typing http://localhost/myproject/index.php/user/register
just type http://localhost/myproject/user/register only

Note:

You need to specify the name of extra folder on your .htaccess files that are located on your project main folder.
         In our htaccess, line RewriteCond $1 !^(index\.php|images|robots\.txt), contains ‘images’, that is the folder on your project. If you want to create more folders for keeping images, JavaScript, css, etc., then you must specify them on your .htaccess. else you cannot access into your folders.

Example:

RewriteCond $1 !^(index\.php|images|css|js|others|robots\.txt)

Where images, css, js and others are the folders on your project.

htaccess option for folder security and removal of index.php

Download .htacess file 

If you can’t setup .htaccess using above steps, then you can download a sample .htaccess file from here. Then copy the file into your project folder.

Note that, you must change the project name into yours.

No comments:

Post a Comment