Codeigniter - SPLessons

Codeigniter htaccess

Home > Lesson > Chapter 11
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Codeigniter htaccess

Codeigniter htaccess

shape Introduction

Normally, CodeIgniter .htaccess is nothing but "Hypertext Access" and these files are also called as Distributed Configuration Files which helps in making changes to the configuration on a per-directory basis. Suppose, if a file having multiple directives for configuration, then that is placed in a document directory, and made to apply directives on that directory, and the process continues for sub-directories also.

shape Description

Codeigniter htaccess files are used by web servers like Apache to control different server features. So when focused on the URL, there is index.php which comes by default and becomes drawback when searched by the search engines for the actual URL instead of using index.php in every URL. Hence, index.php has to be removed.

Remove index.php using CodeIgniter htaccess

shape Description

Initially, the CodeIgniter Welcome Page comes by default when used the below URL's. [html] http://localhost/Codeigniter/ or http://localhost/Codeigniter/index.php/welcome [/html] In the above URL's the second URL doesn't work out if removed index.php and the result will be as below. To overcome the problem, index.php have to be removed which involves the following steps. Step-1: Open the file config.php file in the path application/config. Check for the below code and remove index.php from $config['index_page'] = ''; [php] // Find the below code $config['index_page'] = "index.php"; // Remove index.php and save $config['index_page'] = ""; [/php] Step-2: Next go to the root folder of the application and create a file .htaccess as shown below. Step-3: Now enter the below code into Codeigniter htaccess file. [html] <IfModule mod_rewrite.c> RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php/$1 [L] </IfModule> [/html] Step-4: Sometimes in config.php file, there will be default setting for uri_protocol as AUTO which does not work properly. To solve the problem replace the line as shown below. [php] // Find the below code $config['uri_protocol'] = "AUTO"; // Edit this line as $config['uri_protocol'] = "REQUEST_URI" ; [/php] Step-5: Now enter the URL http://localhost/Codeigniter/welcome and check the output which works fine even after removal of index.php. This is the usage of .htaccess file.

Summary

shape Key Points

  • Codeigniter htaccess is "Hypertext Access".
  • Remove index.php in CodeIgniter URL using .htaccess file.