Pages

Saturday, December 10, 2016

Removing index.php from codeigniter URI !!!

now everyone have being go trough this problem which is index.php in my url, 


so the question here is how we can remove ?

FIRST: Can i remove index.php without .htaccess?
The Answer is    NO. it is something beyond php or codeigniter, its a server thing.  WHY ??!!

What happens when user try to go to localhost/mail/Users/login ?
MVC basically hide all your php files inside its folders. and present only one way for any user to interact with your code through index.php
what happen is
  1. HTTP request to your website directed to index.php
  2. index.php load codeigniter bootstrap and call codeigniter router
  3. router look up the url typed (welcome/index) then go fetch the file welcome from inside controller folder and call index function.
this is plain simple what all MVC frameworks do, hide your code and use a single front file to handle all requests and redirect them using a router class.
so if you want to hide index.php then you need to find a way to tell your server to simply redirect any call to index.php.
index.php is the access door for your website, and your server need to know that all requests must be sent to it. some way or another, and thats why index.php/ must exisit in ur url or use some server configuration to redirect calls to it. but it is always loaded

SO HOW WE CAN REMOVE IT ?
I have use following method, it is working perfectly in UBUNTU...!!
First of all verify that you have enabled mod_rewrite (you can verify using phpinfo()) in your index.php file.
IF DISABLED mod_rewrite then RUN: 
  •  sudo a2enmod rewrite 
  •  sudo gedit /etc/apache2/sites-available/defaut just replace "AllowOverride None" to "AllowOverride All" in Tag(<'Directory /var/www/'>) 
  •  Then please run sudo service apache2 restart

Ok Good... Now in your root directory of Codeigniter -> Find .htaccess file, if not found then press ctrl+h , still not found then create a file .htaccess
In .htaccess Paste following code :
<IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteBase /ROOT DIRECTORY NAME/

        # Directs all EE web requests through the site index file
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
Ok finally just replace ROOT DIRECTORY NAME with your site name.
Then go to application/config/config.php file and change the following:
 $config['index_page'] = 'index.php';
TO
 $config['index_page'] = '';
NOW lets descripe what we just wrote in our .htaccess file:
RewriteEngine On
Description: Enables or disables runtime rewriting engine
Syntax: RewriteEngine on|off

        # Directs all EE web requests through the site index file
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond basically means "execute the next RewriteRule only if this is true". The !-l path is the condition that the request is not for a link (! means not, -l means link)
The RewriteRule basically means that if the request is done that matches ^(.+)$ (matches any URL except the server root), it will be rewritten as index.php?url=$1 which means a request for ollewill be rewritten as index.php?url=olle.
QSA means that if there's a query string passed with the original URL, it will be appended to the rewrite (olle?p=1 will be rewritten as index.php?url=olle&p=1.
L means if the rule matches, don't process any more RewriteRules below this one.
Thanks for reading.

No comments:

Post a Comment