user1275378
user1275378

Reputation:

CodeIgniter Remove index.php in URL

I've created a codeigniter project in Ubuntu system and added following base_url as follows.

http://192.168.1.123/project/

If I access any other url like as follows.

http://192.168.1.123/project/course/view/1

That redirects to 'Page Not Found'. If I include index.php after base_url, Its working fine.

And I created .htaccess file as follows.

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

In config.php file

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

changed to

$config['index_page'] = '';

But still its not working. Please suggest soultion. The work would be appreciated.

Upvotes: 0

Views: 6723

Answers (5)

Martin Mbae
Martin Mbae

Reputation: 1266

For those using Linux, this are the steps to follow

Open the .htaccess file in the root folder of your project. This is located on the same folder with application folder.

Edit the content of .htaccess to

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

open the config file and change

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

to

$config['index_page'] = '';

Navigate to etc/apache2 and open a folder called apache2.conf

Edit the AllowOverride from None to All as shown below

<Directory /var/www/>
    Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted

</Directory>

to

<Directory /var/www/>
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted

</Directory>

Open terminal and run this command

sudo a2enmod rewrite

To activate the new configurations restart apache by using this command

systemctl restart apache2

You have now successfully removed index.php from your url

Upvotes: 0

Dino Babu
Dino Babu

Reputation: 5809

Step 1 :

Add this in htaccess file

<IfModule mod_rewrite.c>
  RewriteEngine On
  #RewriteBase /

  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteRule ^ index.php [QSA,L]
</IfModule>

Step 2 :

Remove index.php in codeigniter config

$config['base_url'] = ''; 
$config['index_page'] = '';

Step 3 :

Allow overriding htaccess in Apache Configuration (Command)

sudo nano /etc/apache2/apache2.conf

and edit the file & change to

AllowOverride All

for www folder

Step 4 :

Enabled apache mod rewrite (Command)

sudo a2enmod rewrite

Step 5 :

Restart Apache (Command)

sudo /etc/init.d/apache2 restart

Upvotes: 3

user762267
user762267

Reputation: 79

Firstly put .htaccess file outside application folder.

Then put following code in .htaccess file

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.*)$ index.php/$1 [L]

Now you can refresh your url without index.php and it will work surely

Upvotes: 0

user1823693
user1823693

Reputation:

Try with this:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /project/
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /project/index.php [L]
</IfModule>

And, in the config.php:

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

Please, make sure the mod_rewrite module is enabled

Upvotes: 0

Lokesh Jain
Lokesh Jain

Reputation: 579

change in .htaccess file and try it

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

Upvotes: 4

Related Questions