Yogesh Chauhan's Blog

How to control file extensions with an .htaccess file?

in Miscellaneous on March 14, 2021

You must have many websites showing index.html file name when you load any page from their website. How can they have all pages names as index.html or just *.html? Do they manually write down the HTML code? Definitely not. All they do is change the original file extension with .html or any other extension to hide the original extension of the file.

One way to do is using .htaccess file for Apache or web servers.

Where is the .htaccess file located?

The .htaccess file should be located in your site’s root directory.

If you’re using FTP or SFTP to load the files from your website to your local machine then sometimes you might not see the .htaccess file as it might be hidden. You need to change your hosting settings to make it visible.

If you don’t have .htaccess file on your web server, you can always create one. Just create a blank text file and save it as .htaccess.

Here’s how to change the file extension

Just add the following code to your .htaccess file and change the extension to your desired extension.

The following code will change the file extension from a .php to .html.

Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteRule ^(.+)\.html$ /$1.php [NC,L]

When you change the file extension, you can access the file using both of those extensions. For e.g. you can access the file using .html and .php after adding the code above in your .htaccess file.

Here’s how to remove the extensions completely.

Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 How to add Read More Read Less Button using JavaScript? #3 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #4 How to uninstall Cocoapods from the Mac OS? #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to Use SQL MAX() Function with Dates?

Recently Posted

#Jan 17 4 advanced ways to search Colleague #Jan 16 Colleague UI Basics: The Search Area #Jan 16 Colleague UI Basics: The Context Area #Jan 16 Colleague UI Basics: Accessing the user interface #Jan 14 How to display a student’s individual transcript in Colleague? #Jan 11 How to install PuTTY on a MacOS?
You might also like these
Killing A Project Part 3: How can an organization ensure that a doomed project is killed as early as possible?MiscellaneousWhat happened to your Google Analytics Tracking ID, can’t find it?MiscellaneousComposing and Extracting Components in ReactReactHow to send and receive query strings via links in Angular 9?AngularThe fundamental difference between HAVING and WHERE clauses in PostgresPostgresSequence generator (range) using JavaScript Array.from()JavaScript