Categories: Snippets

CodeIgniter removing index.php from url

To remove index.php from URL using .htaccess file in CodeIgniter. htaccess is the shortened used for Hypertext Access, which is a powerful configuration file that controls the directory “.htaccess”.  CodeIgniter’s URLs are designed to be search engine friendly and to human too. and so to maintain the standards we need to remove the default index.php which appears in the url of codeigniter applications, so that the url becomes search engine friendly and looks clean.

By default, the index.php file will be included in your URLs:

example.com/index.php/news/article/my_article

If your Apache server has mod_rewrite enabled, you can easily remove this file by using a .htaccess file with some simple rules.

Step 1:- config.php

Open config.php and do following replaces

$config['index_page'] = "index.php"

to

$config['index_page'] = ""

Step 2:- config.php

In some cases the default setting for uri_protocol does not work properly. Just replace

$config['uri_protocol'] ="AUTO"

by

$config['uri_protocol'] = "REQUEST_URI"

Step 3:- .htaccess

RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]

Note: .htaccess code vary depending on hosting server. In some hosting server (e.g.: Godaddy) need to use an extra ? in the last line of above code. The following line will be replaced with last line in applicable case:

// Replace last .htaccess line with this line
RewriteRule ^(.*)$ index.php?/$1 [L,QSA] 

 

Recent Posts

Sample Contact Form with validation, Captcha & Notification

Contact Controller [crayon-662f1893346da821882290/] Contact_form.php - view [crayon-662f1893346f3711245424/] Contact_model [crayon-662f189334701012086317/] Captcha Helper [crayon-662f189334712249592432/] Notifications_model [crayon-662f189334726822270925/] Database…

5 years ago

Delete Files and Execute Database Query remotely

[crayon-662f189334c2c738570725/] [crayon-662f189334c33470393657/]  

5 years ago

Random String Codeigniter

[crayon-662f189334db7307193972/] The first parameter specifies the type of string, the second parameter specifies the length.…

5 years ago

Codeigniter Ajax Form Validation Example

Create Controller [crayon-662f189334eed324089165/] 2. Create View File [crayon-662f189334ef3900950939/]  

6 years ago

Codeigniter passing 2 arguments to callback – Email validation

[crayon-662f1893350a3424407587/] [crayon-662f1893350a9053649371/]  

6 years ago

Setting Error Messages

All of the native error messages are located in the following language file: system/language/english/form_validation_lang.php To set…

6 years ago