how to remove index.php from URL in apache

How to Remove index.php from URL

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.


How to Remove index.php from URL

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

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.

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

mm

About Ubiq

Ubiq is a powerful dashboard & reporting platform for small & medium businesses. Build dashboards, charts & reports for your business in minutes. Get insights from data quickly. Try it for free today!