Home  >  Article  >  Backend Development  >  How to Remove 'index.php' from URLs in CodeIgniter?

How to Remove 'index.php' from URLs in CodeIgniter?

Linda Hamilton
Linda HamiltonOriginal
2024-11-13 03:31:02283browse

How to Remove

CodeIgniter htaccess and URL Rewrite Issues

Problem:

For a beginner using CodeIgniter, removing "index.php" from URLs while accessing pages like "about" (e.g., "localhost/ci/about") remains elusive. Despite experimenting with configurations and searching for solutions, the desired functionality is still not achieved.

Answer:

To resolve this issue and clean up URL rewrites, follow these steps:

1. Configuration in application/config.php:

  • Set $config['base_url'] to the actual URL of your CodeIgniter installation.
  • Set $config['index_page'] to an empty string ''.
  • Set $config['uri_protocol'] to 'AUTO'.

2. .htaccess in the root directory:

Create a .htaccess file in the root directory of your CodeIgniter installation with the following content:

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

3. Enabling the rewrite engine in Apache (if not already enabled):

  • Run a2enmod rewrite to initiate the rewrite module.
  • Edit /etc/apache2/sites-enabled/000-default and change all "AllowOverride None" to "AllowOverride All".
  • Restart Apache using sudo /etc/init.d/apache2 restart.

After completing these steps, you should now be able to access your pages without the "index.php" in the URL. For example, "localhost/ci/about" should successfully load the "about_page.php" view.

The above is the detailed content of How to Remove 'index.php' from URLs in CodeIgniter?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn