How to eliminate the .html extension from the address bar of a web browser? The website is static and hosted, containing folders with nested HTML pages.
If you are using Apache as your web server on the hosting platform, you can simply add the following lines to the .htaccess file:
RewriteEngine on
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^.]+)\.html\ HTTP
RewriteRule ^([^.]+)\.html$ site.com/$1 [R=301,L]
RewriteCond %{REQUEST_URI} !(\.[^./]+)$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) /$1.html
Make sure to replace "site.com" with the actual domain name of your website. With these configurations, accessing site.com/contacts.html will automatically redirect to site.com/contacts.
By utilizing such rules, you can create cleaner and more user-friendly URLs for your website. It not only enhances the user experience but also improves the search engine optimization of your site.
If you don't have access to HTACCESS on the hosting, unfortunately, there is no way to proceed in this direction. However, if you do have access, it's worth exploring this option.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
This process involves using URL rewriting techniques to create cleaner, more user-friendly URLs.
1. Create or Modify the .htaccess File: The .htaccess file is a configuration file used by the Apache web server to apply directory-level configuration settings. If your website is hosted on an Apache web server, you can use the .htaccess file to implement URL rewriting. If you don't already have a .htaccess file in the root directory of your website, you can create one.
2. Enable the Rewrite Engine: Within the .htaccess file, you will need to enable the mod_rewrite engine by adding the following line:
```apache
RewriteEngine On
```
3. Redirect .html URLs to Non-Extension URLs: You can use the RewriteRule directive to create a rule that redirects requests for .html files to the corresponding non-extension URLs. For example, if you have a page called "about.html", you can create a rule to redirect requests for "example.com/about.html" to "example.com/about". Here's an example of how you might accomplish this:
```apache
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
```
In this rule, the first RewriteCond checks if the requested URL is not a directory, the second RewriteCond checks if the corresponding .html file exists, and the RewriteRule performs the actual redirection.
4. Update Internal Links: After implementing the rewrite rule, you will need to update the internal links within your website to point to the non-extension URLs. This includes updating navigation menus, anchor tags, and any other links that reference HTML files. For example, you would change a link from "example.com/page.html" to "example.com/page".
5. Test and Validate: It's crucial to thoroughly test the website to ensure that the URLs are working as expected. Check for broken links, ensure that the pages load correctly, and verify that there are no errors or unexpected behaviors. You can use browser developer tools or online validation tools to check the rewritten URLs.