Showing posts with label CodeIgniter. Show all posts
Showing posts with label CodeIgniter. Show all posts

Thursday, January 21, 2010

CodeIgniter - Remove index.php from URI

Normally with CodeIgniter, the URI will come with http://domain/index.php/controller/method
You may have doubt to remove the index.php to have only http://domain/controller/method ..

Here is the way:
1. mod_rewrite still need to be enable in your apache server
2. Open config.php from your application/config and replace $config['index_page'] = “index.php” by $config['index_page'] = “”

3. In file .htaccess at the root of your website (should be in website root, where the system directory is) and add following line:


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

(My case, above changes work fine)

4. In some case, the default setting for uri_protocol does not work properly. To solve this problem just replace $config['uri_protocol'] = “AUTO” by $config['uri_protocol'] = “REQUEST_URI” from application/config/config.php

Enjoy,

Update 26/01: If you want both using index.php with URI or no, you just need to skip option 4 (using uri_protocol as AUTO)

CodeIgniter - Automatic config base url

In CodeIgniter, you may know that you need to configure base url via config.php (param: $config["base_url"]) so when change domain you may doubt sometimes with such small error; Here a solution to help solving the issue;

Change at config.php by fix url with this smart url configuration:

$config['base_url'] = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == "on") ? "https" : "http");
$config['base_url'] .= "://".$_SERVER['HTTP_HOST'];
$config['base_url'] .= str_replace(basename($_SERVER['SCRIPT_NAME']),"",$_SERVER['SCRIPT_NAME']);

Source: http://codeigniter.com/wiki/Automatic_configbase_url/