How to Remove file extension from URL using htaccess

how to Remove file extension from URL using htaccess file

Many times you want to remove the URL extension (. php, . html, .jsp, etc.) from your website. Example

https://www.yourdomain.com/index.php  https://www.yourdomain.com/index

In this tutorial, I will show how to Remove file extension from URL using htaccess file.

.htaccess file is a server configuration file.  the file contains below server configurations

1. Project directory
2. Page redirection from one to another configuration
3. Block Suspicious IP traffic
4. Rewrite URLs
5. Remove URLs extension like (.php, .html etc)
6. password protect your website directory

Following the steps to Remove file extension from URL using htaccess

1. Login to your server via FileZilla FTP
2. Go to the project directory
3. Create .htaccess file on your project directory and add the below following code inside the .htaccess file. This file can be easily edited in any text editor.

This code is remove the .php extension from your website

# This code remove the extension from your URLs
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php

# Return 404 if original request is .php
RewriteCond %{THE_REQUEST} “^[^ ]* .*?\.php[? ].*$”
RewriteRule .* – [L,R=404]

If you want to remove the .html extension from html file from https://www.yourdomain.com/index.html to https://www.yourdomain.com/index. you need to add the below code inside your .htaccess file.

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*)$ $1.html

If you are using the Godaddy server then you need to add the MultiViews tag before starting. Example.

Options +MultiViews
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php

After configuration, If the user enters the URL as https://www.yourdomain.com/index.html it will automatically redirect to https://www.yourdomain.com/index

Download the sample .htaccess file and you can also visit stackoverflow.com