Hide or Remove php or html File Extension Using htaccess
Hide or Remove php or html File Extension Using htaccess
July 04, 2020

You might have seen the extensions including .php, .html, or .htm while accessing the web pages. It's because of the webserver directly serving the file without hiding the extension. We can hide or remove the file extension from the URL by adding the .htaccess file at the required path. The most common path is to add the .htaccess file at the root of the web application.

Prerequisites

This tutorial assumes that the Apache Web Server is already installed on either the local or remote system. It also assumes that you have access to the root directory of the web application.

Add .htaccess

Now add the .htaccess file at the root of the web application and update it to redirect all the requests by appending the appropriate extension among php, html, or htm as shown below.

# Add/Update .htaccess 
sudo nano /var/www/example.com/html/.htaccess

# Update
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

# Save and Exit - Ctrl + o -> Enter -> Ctrl + x

The above configurations enable rewrite module and specify a rewrite condition that checks for the full local path and the rewrite rule appends the extension .php for the matching conditions. Instead of php extension, we can update the rewrite rule and use the other extensions including html and htm.

RewriteCond Directive

Syntax - RewriteCond TestString CondPattern [flags]

RewriteCond - The directive

TestString: %{REQUEST_FILENAME} - The full local filesystem path to the file or script matching the request

CondPattern: ! - The NOT character can be used to reverse the condition

Flag: -f - It considers the TestString as a pathname and tests whether or not it exists, and is a regular file

Write a Comment

Click on the captcha image to get new code.
Discussion Forum by DISQUS