How to remove .php, .html, extensions using .htaccess.


What is a .htaccess file?

  • The .htaccess file is a simple ASCII file that which is created with a Simple text editor like any notepad. You make configuration changes in the server using a .htaccess file.

  • The .htaccess is just a file’s extension. It's not a file .htaccess, it is a simple .htaccess file.

  • The .htaccess files can be uploaded to all sub-directories and main directories.

  • For example, if you uploaded any .htaccess file located in your root directory of Domain.com, it would affect Domain.com/content/, Domain.com/content/images/, etc.

 

How the .htaccess file helps you?

  • The .htaccess make the URLs more user and search engine friendly.

  • You can remove the extensions like .php, .html, .htm, with the help of .htaccess

  • You can Redirect the user to different page or domain.

  • Blocking the Ip by countries or series of ips.

  • password protection for the specific Directories.

  • Rewrite URL's

 

In this article, we will see How to remove the Extensions?

  • Removing Extensions

To remove the .php extension from a file, for example, Domain.com/admin.php to Domain.com/admin.php you have add the below mentioned code in the .htaccess file.

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f

RewriteRule ^([^\.]+)$ $1.php [NC,L]


To remove the .html extension from a file, for example, Domain.com/admin.html to Domain.com/admin.html you have add the below mentioned code in the .htaccess file.
RewriteRule ^([^\.]+)$ $1.html [NC,L]



That's it, now you can remove the extension by adding the code to the .htaccess file.

Why do you want to remove the .php or .html from the URL ?



1. You can remove the .php or .html extension from the URL to give a uniform look and feel throughout the website. Better and clean URLs will give a good impression for the users.

2. You can hide the implementation language from the hackers, when you have .php or .perl or something else, then it is easy for others to know what is the programming language used.

3. You can come up with SEO friendly URL.