Step 1: Enable Mod Rewrite Option in your APACHE Server
To rewrite urls in the htaccess file, the mod_rewrite option should be enabled in the apache server. Goto apache's "httpd.conf" file and search for the line,
LoadModule rewrite_module modules/mod_rewrite.so
If the above line is preceded with # (in which case, the module is disabled), then remove the # (hash) symbol to enable url rewriting.
Step 2: Create '.htaccess' File
Next create the .htaccess file at the root directory. To create the htaccess file, open your favourite text editor and create a new text file. Save it as ".htaccess" (make sure you type the filename within quotes to avoid the file to be saved as text file) in the root directory.
Now copy paste this code to the htaccess file and save it.
<IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC] RewriteCond %{REQUEST_URI} !/system/.* [NC] RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ /index.php/$1 [L] </IfModule>
If you run your codeigniter app as a subdirectory instead of root domain like,
http://example.com/cisite/
instead of http://example.com/
then the above code won't work. You have to tweak it little to tell that your site runs as a subdirectory. Add this code to the htaccess file instead of the above one.<IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC] RewriteCond %{REQUEST_URI} !/system/.* [NC] RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php/$1 [L] </IfModule>
Step 3: Modify CodeIgniter Config Settings
Open config.php file under application >> config folder and search for the line,
$config['index_page'] = 'index.php';
Remove index.php from it to look like this,
$config['index_page'] = '';
Now restart apache and check the url. eg.,
http://www.example.com/contact
instead of http://www.example.com/index.php/contact
Recommended Read: How to Upload Files in PHP Securely
Recommended Read: How to Insert JSON into MySQL using PHP
If it doesn't work, you may be having problem with uri protocol. In that case find
$config['uri_protocol'] ='AUTO'
setting in config file and replace with $config['uri_protocol'] = 'REQUEST_URI'
.
Restart apache server again and you can check the codeigniter site to see that index.php has been removed from codeigniter url.
No comments:
Post a Comment