Enable SEF URL in Codeigniter
We are familiar with Codeigniter comes with optional SEF URLs, but we use non SEF urls in production or development version.
1 2 | http://localhost/index.php/helloworld/welcome/ http://localhost/helloworld/welcom/ |
In the above snippet we know that line 1 is default URL which contains index.php in URl part that used in development version.
Second line is the deployed version SEF URL, generally we need some tweaks with .htaccess and configure stuff.
htaccess
Open note pad and copy and paste below code into that and save it as .htaccess and copy this file into your codeigniters root directory where it was installed.
1 2 3 4 5 6 | DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|images|css|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/$1 [L,QSA] |
Line 1: set index.php as default directory index instead of index.html
Line 2: Start rewriting engine on
Line 3: start a condition that won’t meet this situation where the request to static resources like images , css, javascript , robot text files
Line 4: make condition user requests a valid request
Line 5: make condition no physical file exists with the given URL
Line 6: if above conditions all met redirect the request to index.php on installation
making changes to config.php file
open the file in the directory system/application/config/config.php make following changes.
Find below line
$config['index_page'] = "index.php";
Replace above line with below code
$config['index_page'] = "";
Now test the new URLs ,it done.
