How to Remove .php and .html from URL in Apache

remove .php and .html from url in apache

When you run a PHP website on Apache, it is very likely that most of your URLs show .php and .html extensions. This is not very user-friendly and can also reveal your backend technology to malicious visitors. Here’s how to remove .php and .html from URL in Apache.


How to Remove .php and .html from URL in Apache

Here are the steps to remove .php and .html from URL in Apache.


1. Ensure mod_rewrite is enabled

Before you proceed, please ensure that you have enabled mod_rewrite in your Apache server.

$ sudo apachectl -M

This will list all the modules enabled in your Apache server. mod_rewrite should be one of them. Otherwise, here are the steps to enable mod_rewrite:


2. Update .htaccess file

Once you enable mod_rewrite in Apache, you can add the following lines of code to the .htaccess file in its root (/var/www/html) location. If there is no .htaccess file there, just create a blank file named .htaccess at that location.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule (.*) $1.php [L]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule (.*) $1.html [L]

The first line enables the URL rewrite feature. The next line checks to see that the requested URL is an actual web page address and not a file. File requests are not processed.

The third line represents the URL rewrite whereby .php extensions are removed from requested URLs and rewritten. So you won’t see .php extensions in requested URLs.

The next 2 lines repeat the above process for .html extension


3. Restart Apache server

Restart Apache web server to apply changes

$ sudo service apache2 restart



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!