Home >Backend Development >PHP Tutorial >How to Eliminate 'index.php' from CodeIgniter URLs: A Guide to .htaccess and URL Rewriting

How to Eliminate 'index.php' from CodeIgniter URLs: A Guide to .htaccess and URL Rewriting

Linda Hamilton
Linda HamiltonOriginal
2024-11-12 14:08:02217browse

How to Eliminate

CodeIgniter htaccess and URL Rewrite Issues

Removing the "index.php" segment from URLs in CodeIgniter can be a common challenge for beginners. Let's delve into the steps involved and troubleshooting potential issues.

Setup

To achieve the desired URL structure, three steps are crucial:

  1. Edit application/config.php

Update the following configuration settings:

$config['base_url'] = 'http://'.$_SERVER['SERVER_NAME'].'/Your Ci folder_name';
$config['index_page'] = '';
$config['uri_protocol'] = 'AUTO';
  1. Create a .htaccess file in the root directory

Add the following code to the .htaccess file:

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

A. Apache Web Server

  • Using the command line: a2enmod rewrite
  • Edit /etc/apache2/sites-enabled/000-default.
  • Change "AllowOverride None" to "AllowOverride All."
  • Restart Apache: sudo /etc/init.d/apache2 restart

Troubleshooting

500 Internal Server Error:

  • Ensure that the .htaccess file is placed in the root directory.
  • Check the syntax of the .htaccess file.

Still unable to access pages without the "index.php" segment:

  • Verify that you have updated the configuration settings in application/config.php and created the .htaccess file correctly.
  • Check the file permissions of the .htaccess file. It should have the appropriate write permissions.
  • Reload the Apache configuration: sudo /etc/init.d/apache2 reload

The above is the detailed content of How to Eliminate 'index.php' from CodeIgniter URLs: A Guide to .htaccess and URL Rewriting. 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