Open In App

How to remove .html extension from URL of a static page ?

All the pages on the website have a structure that is given by HTML. HTML provides the structure to content, text, tables, headings, and lists on the webpage which makes the page easy to read. While saving an HTML document it has an extension as .html. Therefore, the URL of the website has a .html extension. The .html extension can be easily removed by editing the .htaccess file.

.htaccess file

The .htaccess file is created with a Text Editor like Notepad. It is a simple ASCII file that lets the server know what configuration changes are to be made on a per-directory basis.



Note: The .htaccess is the full name of the file. It is not file.htaccess, it is simply .htaccess.

Removing .html Extension

// From  

example.com/content.html

// to
example.com/content

Steps: 




#remove html file extension https://example.com/page.html
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC, L]

You can now link any page inside the HTML document without needing to add the extension of the page as no extension will be visible now in the URL of the website.



Example:  

<a href="http://example.com/image" title="image">image</a>

The search engine may index these pages as duplicate content, to overcome this add a <canonical> meta tag in the HTML file. 

Example:  

<link rel="canonical" href="https://example.com/blog/first-blog" />

 

Article Tags :