Last updated on August 28th, 2024 at 04:22 am
By default, Apache adds .html to URL for HTML files. But it can be difficult for users to remember to add this extension in request URLs, which may result in ‘Page Not Found’ errors. Therefore, it is advisable to remove these extensions from your website’s URLs. Here’s how to remove .html from URL in Apache. You can use these steps to remove .html from URL in WordPress, Magento, Drupal, Joomla and other CMS systems. You can also use it to remove index.html from URLs.
Why Remove .html Extension
By default, most websites and content management systems (CMS) add URL extensions such as .html, .php, .asp, .jsp after the URL. Users are also required to add the extension in their requests. But these days, nobody adds URL extensions anymore. So if users do not add URL extensions in their request, your web server will see them as a different URL and return ‘404: Page Not Found’ error message. Also, any malicious user will be able to quickly figure out your technology by just looking at the URL extension. Therefore, it is recommended to remove URL extensions from your website, when you begin running it.
Remove .html extension from URL
Before proceeding further, you need to enable mod_rewrite in Apache web server. Here are the steps to enable mod_rewrite (.htaccess) in Apache web server.
1. Open htaccess file
Open terminal and run the following command to open .htaccess file. Please change the file path to .htaccess file as per your requirement.
$ sudo vi /var/www/html/.htaccess
Bonus Read : How to Remove .php from URL
2. Remove .html extension from URL
Add the following lines to redirect all URLs with .html extension to URLs without file extension.
RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME}\.html -f RewriteRule ^(.*)$ $1.html [NC,L]
In the above code, the first line checks that the request is a URL and not a directory. The second line redirect matching URLs with .html extension to those without file extension. The last line redirects matching requests to URLs without html file extension. It is similar to removing index.php from URL in Apache/PHP.
Please note, you can use the same method to remove other extensions too such as .php, .asp, .jsp from requested URL. Just replace .html in above code with .php or extension of your choice.
Here is an example to remove .php from URLs.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
Bonus Read : How to Disable mod_security in Apache
3. Restart Apache web server
Restart Apache server with following command
$ sudo systemctl restart httpd
4. Verify Changes
To make sure that .html extension has been removed from URLs, open web browser and enter any URL with .html extension. You will see that the you are redirected to a URL without the extension. Now whether users enter or do not enter .html extension in request URL, they will be redirected to the right page and not see any error message.
Conclusion
By default, most web servers require you to add URL extensions such as .html or .php in requests. Since users may not remember to add them, it may result in 404:Page Not Found errors. To avoid this problem, it is advisable to remove .html extension from URLs in your web server, when you host your website itself, if not later. This way even search engines will be able to capture the right URLs without any extension.
Ubiq makes it easy to visualize data in minutes, and monitor in real-time dashboards. Try it Today!
Sreeram Sreenivasan is the Founder of Ubiq. He has helped many Fortune 500 companies in the areas of BI & software development.