Hướng dẫn remove index php

By default, PHP platforms such as WordPress and Codeigniter append index.php at the end of website URL, which is not really required. Here are the steps to remove index.php from URL using .htaccess in Apache web server. You can use these steps to remove index.php from WordPress, Codeigniter and other PHP websites.

Nội dung chính

  • 1. Open .htaccess file
  • 2. Remove index.php from URL
  • 3. Restart Apache web server
  • Related posts:
  • What is CodeIgniter?
  • Why remove index.php?
  • How to remove index.php?
  • How do I remove a site index?
  • Why is index php in my URL?
  • How do I remove a php from a URL?
  • What is .htaccess file in php?

We will basically redirect the index.php to URL without it. For that we will need mod_rewrite Apache module.

Before proceeding further, please ensure that you have enabled mod_rewrite in Apache web server. Here are the steps to enable mod_rewrite in Apache. At the end of it, you will have created a .htaccess file for your website.

1. Open .htaccess file

Open terminal and run the following commands to open .htaccess file. We have used the default file path of .htaccess file. You can change it as per your requirement.

$ sudo vi /var/www/html/.htaccess

2. Remove index.php from URL

Add the following lines in .htaccess file.

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

Let us look at each line above. The first line enables mod_rewrite if it is not enabled already.

The second line matches URLs that are not files. The third line matches URLs that are not directories. So we are looking to match URLs that are neither files nor directories.

The last line means that if the two RewriteCond statements before it are matched for a URL, then it will be redirected to URL without index.php in it.

3. Restart Apache web server

Restart Apache server to apply changes

$ sudo systemctl restart httpd

Now if you open browser and visit http://your_domain_or_IP/index.php you will be redirected to http://your_domain_or_IP. Replace your_domain_or_IP with your domain or IP address. Similarly, if you visit URL like http://your_domain_or_IP/index.php/folder1/folder2 you will be redirected to http://your_domain_or_IP/folder1/folder2

  • About Author

In the CodeIgniter project, the index.php file will be included in the URL by default but to make it search engine friendly and user-friendly, we need to remove it. In this article, we will learn how to remove index.php in Codeigniter.

Following pointers will be covered in this article:

  • What is CodeIgniter?

  • Why remove index.php?

  • How to remove index.php?

Let’s begin.

What is CodeIgniter?

CodeIgniter is a PHP driven framework for developing applications rapidly. While building a web application, we spend a lot of time writing the same code again and again. Frameworks provide us a starting block and minimize the amount of code needed to build a website. CodeIgniter is a free, open-source, easy-to-use, object-oriented PHP web application framework. CodeIgniter is based on the MVC, it has libraries for connecting to the database and performing various operations like sending emails, uploading files, etc.

For using Codeigniter, it is good to have some basic knowledge of PHP syntax and how to interact with databases and HTML.

Why remove index.php?

Removing index.php makes the URL look cleaner and professional. The Codeigniter framework served all the views through the one file index.php. Without this file, no model/views/controller won’t work. We need to remove the index.php from URL with proper configuration or else it will show 404 page.

How to remove index.php?

1. Create a .htaccess file

Firstly we will need to create a .htaccess file in our project’s root directory or CodeIgniter directory. Creating a .htaccess file will allow us to modify our rewrite rules without accessing server configuration files. Because of this reason, .htaccess is critical to our web application’s security thus ensures that the file is hidden. htaccess is an acronym used for Hypertext Access, it is a configuration file that controls the directory “.htaccess”.

After creating the .htaccess file, add the following code to this file.

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

2. Removing index.php file

Open the config.php file in your text editor and remove index.php from here.

Change:

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

To:

$config['index_page'] = '';

But in a few cases, it may happen that the default setting for url_protocol does not work properly. For solving this problem we need to open config.php and

Change:

$config['uri_protocol'] = "AUTO";

To:

$config['uri_protocol'] = "REQUEST_URI";

Most of the time it is already set to REQUEST_URI, In that case, we don’t need to change it.

3. Enable apache mode rewrite

Now, we will need to activate the mod_rewrite with the help of the following command.

$ sudo a2enmod rewrite

4. Restart Apache

Now we need to restart the Apache, to do so we write the following command.

$ sudo service apache2 restart

This will activate the module or alert us that the module is already in effect. 

Now with this, we have come to the end of the article. I hope you guys enjoyed it and understood the concepts of PHP. So, with the end of this tutorial, you are no longer a newbie to the scripting language.

If you found this PHP Tutorial blog relevant, check out the PHP Certification Training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe.

Got a question for us? Please mention it in the comments section of ”How to remove index.php in Codeigniter” and I will get back to you.

How do I remove a site index?

htaccess file for your website..

Open . htaccess file. Open terminal and run the following commands to open . ... .

Remove index. php from URL. Add the following lines in . ... .

Restart Apache web server. Restart Apache server to apply changes $ sudo systemctl restart httpd..

Why is index php in my URL?

The first reason why index. php appears in the URL might be because the structure of permalinks is not set properly in WordPress Settings. So to verify if the structure of permalinks is set properly, let's check the permalink tab in WordPress Dashboard.

How do I remove a php from a URL?

How to Remove ..

Open htaccess file. Open terminal and run the following command to open . htaccess file. ... .

Remove . php extension from URL. ... .

Restart Apache web server. Restart Apache server with following command $ sudo systemctl restart httpd..

What is .htaccess file in php?

htaccess file is created in order to enable extra features for that subdirectory. You can use the . htaccess file to modify various configurations and thus make changes to your website. These changes include authorization, error handling, redirects for specific URLs, user permissions, etc.