Hide Html Extension On Nginx

Disclaimer – This article is for informational purposes only. The author does not make any representations or warranties as to accuracy, completeness, or the results obtained from any information provided.

Hide HTML Extension On Nginx

What is Nginx?

Nginx is an open-source server software that is used to offer web services. It is a popular choice among developers due to its high performance, scalability, low-resource usage, and stability. Nginx is a reverse proxy, meaning it acts as a proxy server, transferring requests from clients to other web servers. Nginx can be used for hosting static websites as well as dynamic websites. It can also be used for caching and load balancing.

Why Hide HTML extension?

The web pages created with HTML have an extension of ‘.html’ as a default. When visitors visit your website, the URL shows the full file extension in the address bar. If you want to have a short URL, then hiding the file extension will help you to do it. It will make the address of the website easier to remember and type. Moreover, it is beneficial from the SEO point of view, as search engines prefer short URLs.

How to Hide HTML Extension on Nginx with ReWrite Module?

To hide the HTML extension on Nginx, you need to enable the rewrite module by typing in the following command:

nginx -t

Once the rewrite module is enabled, create a file with the name of “.htaccess” in the root directory of the Nginx server and copy the following code into it:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f

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

This will enable you to hide the HTML extension for all the web pages on the website. It is important to note that this code will rewrite all the URLs on the website, which is why it is not recommended to use on websites with hundreds of pages or a large amount of traffic.

How to Hide HTML Extension Using Nginx Config?

If you want to hide HTML extensions for only a few webpages, then it is better to use the Nginx config file. To do this, open the Nginx config file and add the following snippet given below:

location /somepage {

rewrite ^/somepage$ /somepage.html last;

}

The above snippet will rewrite the requested URL ‘somepage’ to ‘somepage.html’ and render the HTML content for the webpage. It will also hide the HTML extension from the address bar. This method is more efficient as it only applies to the webpage specified and not to the entire website.

Other Ways to Hide HTML Extenison?

Apart from using the ReWrite module or Nginx config file to hide the HTML extension, there is also another way to do it, which is to use mod_rewrite module in Apache. This module can be enabled to hide the HTML extension for all the webpages of the website. To enable it, open the Apache configuration file and add the following code:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f

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

Once the code is added to the Apache configuration file, the module will be enabled on the server and the HTML extensions will be hidden from all requesters.

Conclusion

Hiding HTML extensions on Nginx is a simple process. You can either use the ReWrite module or Nginx config file. By hiding the HTML extension, you can make the URL of the website shorter and easier to remember. It is also beneficial from the SEO point of view, as most search engines prefer short URLs. Moreover, it also adds a level of security as the file extension will be hidden from visitors.

FAQs

  • What is Nginx?

    Nginx is an open-source server software that is used to offer web services.

  • How to hide HTML extension on Nginx?

    To hide the HTML extension on Nginx, you need to enable the rewrite module, create a file with the name “.htaccess” in the root directory and add the code given in the article.

  • What is the purpose of hiding HTML extension?

    Hiding the HTML extensions helps in making the URL shorter and easier to remember. It also helps in improving the SEO ranking of the website.

Thank you for reading this article. Please read other articles by clicking the keyword ‘Hide HTML Extension on Nginx’ to learn more about Nginx.

Leave a Reply

Your email address will not be published. Required fields are marked *