How to Remove Index.php from URL in Apache


remove index.php from url in apache

When you run a PHP website on Apache, it is likely that your home page’s URL contains “index.php”. This is not really user-friendly and can also reveal your technology to malicious website visitors. Here’s how to remove index.php from URL in Apache.

 

How to Remove Index.php from URL in Apache

Here are the steps to remove index.php from URL in Apache. Basically, we will rewrite URL using .htaccess file that will hide the index.php part of your URL.

 

Before you proceed, please ensure you have enabled .htaccess file (mod_rewrite) for your Apache server. Here are the steps to do it:

 

Place your .htaccess file in the root document folder of your website (/var/www/html) and add the following rules

 


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

 

The first line enables url rewrite functionality. The next 2 lines checks the URL and skips directories and file locations from processing. The last line removes index.php from it

 

Similarly, if you want to remove index.html, you can add the following code to your .htaccess file.


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

 

Restart Apache Web Server

Restart Apache web server to apply changes

CentOS/Fedora/RHEL


/etc/init.d/httpd restart 

 

Ubuntu/Debian/LinuxMint


sudo /etc/init.d/apache2 restart 

 

That’s it! Now Apache will automatically remove index.php from URL.

About Sreeram Sreenivasan

Sreeram Sreenivasan is the Founder of Ubiq, a business dashboard & reporting platform for small & medium businesses. Ubiq makes it easy to build business dashboards & reports for your business. Try it for free today!