How to Remove index.php from URL

Last updated on August 29th, 2024 at 05:22 am

By default, PHP platforms such as WordPress and Codeigniter append index.php at the end of website URL, which is not really required. Often site administrators and web developers need to remove it from all their URLs. You can easily remove index.php from URL using .htaccess (mod_rewrite) in Apache web server. You can use these steps to remove index.php from WordPress, Codeigniter and other PHP websites.

Why Remove index.php from URL

By default, whenever you run a PHP site on Apache server, or any other web server, the URL of your site’s home page will end with index.php. This is true even if you use a PHP-based web development framework such as CodeIgniter. Therefore, all other URLs on your website will automatically contain index.php. For example, if your site is https://mysite.com, its home page URL will be https://mysite.com/index.php. This is not required since it is difficult to remember URLs with index.php in them. Also it reveals that your site runs on PHP. This might attract malicious attackers who exploit its vulnerabilities.

How to Remove index.php from URL

We will basically redirect the index.php to URL without it, that is, from https://mysite.com/index.php to https://mysite.com. For that we will need mod_rewrite Apache module. mod_rewrite allows you to setup URL redirection and rewrite rules without modifying the main server configuration or virtual host files. In many hosting environments, you may not have access to the main configuration file or virtual host file. So it is important that you use .htaccess file for URL redirection.

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. Please note, you need to create or open .htaccess file present at the root folder of your website. If it is different from /var/www/html, please update the file path in the command below accordingly.

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

Bonus Read : How to Install Fail2Ban in Apache

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.

Similarly, you can also remove .php from URL in Apache by replacing index.php with just .php in the above code.

Bonus Read : How to Install memcached in Apache

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

Conclusion

In this article, we have learnt how to remove index.php from all URLs on our website. They are applicable whether no matter which PHP-based platform you use such as WordPress, Joomla, Drupal, CodeIgniter, etc. It is advisable to make this change when you start hosting your website itself, instead of adding it later on. This will help search engines properly index the right URL and also help users remember them easily.