Search code examples
apache.htaccesscodeigniterhrefcodeigniter-url

CodeIgniter remove index.php from href


In a CodeIgniter App

this link works

<a href="index.php/controller">link</a>

while this one doesn't

<a href="controller">link</a>

What's wrong with configuration? What has to be changed in order links work without "index.php/" at the beginning?

This is done without success:

1) added to application/routes.php

$route['(:any)'] = 'controller';

2) added to application/.htaccess

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

<Files "index.php">
AcceptPathInfo On
</Files>

3) uncommented in etc/apache2/httpd.conf

LoadModule rewrite_module libexec/apache2/mod_rewrite.so
LoadModule php5_module libexec/apache2/libphp5.so

Solution

  • First of all you should blank the

    $config['index_page'] = '';
    

    in config config.php Then write the following code in .htaccess placed in your project's main directory.

    <IfModule mod_rewrite.c>
        RewriteEngine on
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule ^(.*)$ index.php/$1 [L,QSA]
    </IfModule>