8

I need to remove index.php from site_url() return value I don't want search engines cache my URLs contain index.php.

I removed index.php from url by setting .htaccess file but still not working.

For example, when I use site_url() in part of my project, search engines cache the URL like http://url/index.php/controller. I remove index.php from site_url function in system/helper but I think the redirect and form_open functions don't work properly.

3 Answers 3

12

write in your .htaccess file

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]

And set index_page config in config.php

$config['index_page'] = '';
Sign up to request clarification or add additional context in comments.

Comments

5

You can use base_url() function:

echo base_url(); // http://example.com/website
echo site_url(); // http://example.com/website/index.php

1 Comment

base_url() will return http://example.com/website/index.php aswell, if you have $config['index_page'] = 'index.php' set in config.php
1

CodeIgniter 4

The provided answers seems to refer to an older version of CodeIgniter now the configuration are slightly different

  1. Go to /app/Config/App.php
  2. Locate public $indexPage Attribute and change it to:

public $indexPage = '';
  1. Go to /public/.htaccess
  2. Make sure that this block of code is more or less as follow, it should be already set this way by default

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\s\S]*)$ index.php/$1 [L,NC,QSA]

Using .env for configuration

Alternativelly you can use the .env configuration file

  1. Go to .env
  2. Add this to the file

app.indexPage = ''

You can then use methods like without the index.php as trailing file

site_url()
echo anchor('news/local/123', 'My News', 'title="News title"');

Documentation

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.