Tag how to hide the file extension from the url

How to remove .html extension

All the pages on the website have a structure which is given by HTML. HTML provides the structure to content, text, tables, headings, and lists on the webpage that makes the page easy to read. While saving an HTML document it has an extension as .html. Therefore, the URL of the website have 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: To remove the .html extension from a URL. For example: 

example.com/latestpost.html

To

example.com/latestpost

You have to follow these steps: 

  • Log in to cPanel account.
  • In the Files section, click on the File Manager icon.
  • Click on the Settings Button in the top right corner.
  • If you want to make changes in the Primary Domain then Click on the radio button next to the Web Root. If changes are to be made on Other Domains, then Click the dropdown menu and find the domain in which changes are to be made.
  • Remember to check the checkbox next to Show Hidden Files. Now click the Save Button to return to the File Manager window.
  • Now you are in the Root Folder of the domain which you have selected to make changes. Search for the .htaccess file and right-click on it. Click on the Edit option in the menu. You can now add code to the .htaccess file.
  • Add the following code inside the .htaccess file
#remove html file extension https://example.com/page.html
# to https://example.com/page
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC, L]
  • Click on the Save Changes Button and then on Close Button.

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" />

To remove the “.html” extension from URLs, you can use URL rewriting techniques, specifically by configuring your web server to handle such requests.

Here’s an example using Apache’s mod_rewrite module, assuming you’re using an Apache web server:

  1. Enable mod_rewrite: Ensure that the mod_rewrite module is enabled on your server. You can typically do this by uncommenting the relevant line in the Apache configuration file (httpd.conf) or by using the appropriate command for your server setup.
  2. Create or modify the .htaccess file: In the root directory of your website or the directory where the HTML files reside, create or modify the .htaccess file.
  3. Add the rewrite rules: Add the following code to the .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*)$ $1.html [L]

These rules check if the requested URL (without the “.html” extension) matches an existing HTML file. If it does, the server internally maps the URL to the corresponding HTML file with the extension. However, the client sees and accesses the URL without the extension.

  1. Test the configuration: Save the .htaccess file and test the configuration by accessing a URL without the “.html” extension. For example, if your file is “about.html,” you should be able to access it as “https://www.example.com/about.”

Please note that the above instructions are specific to Apache web servers using mod_rewrite. If you’re using a different web server, such as Nginx or Microsoft IIS, the configuration steps may differ. Additionally, ensure that you have appropriate file permissions and consult your server’s documentation for specific instructions.

It’s also worth mentioning that removing file extensions from URLs can have implications for search engine optimization (SEO) and may require updating internal links and references within your website.

Diwali Dhamaka Offer


Coupon code: DIWALI2024 Discount: 7000/-
Coupon expiry date: 2024-11-05

This will close in 20 seconds

X