"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How to Remove "index.php" from CodeIgniter URLs?

How to Remove "index.php" from CodeIgniter URLs?

Published on 2024-11-16
Browse:514

How to Remove

CodeIgniter .htaccess and URL Rewriting Issues

Navigating CodeIgniter applications often requires removing "index.php" from the URL, allowing users to access pages with a cleaner syntax. However, new users may encounter difficulties with this process.

The key to removing "index.php" lies in modifying the application config file (application/config.php) and creating a .htaccess file in the root directory.

1. Application Configuration:

In application/config.php, ensure the following settings:

$config['base_url'] = 'http://'.$_SERVER['SERVER_NAME'].'/Your Ci folder_name';
$config['index_page'] = '';
$config['uri_protocol'] = 'AUTO';

2. .htaccess File:

Create a .htaccess file in the root directory with the following code:

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

3. Rewrite Engine Enablement:

Ensure the rewrite engine is enabled by:

  • Initiating it with "a2enmod rewrite".
  • Changing "AllowOverride None" to "AllowOverride All" in /etc/apache2/sites-enabled/000-default or /etc/apache2/apache2.conf.
  • Restarting the server with the command "sudo /etc/init.d/apache2 restart".

Additional Notes:

If accessing a page via "localhost/ci/about" fails, consider:

  • Using "localhost/ci/index.php/about" or creating an "about" directory in application/.
  • Checking for any directives in the .htaccess file that may be interfering.
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3