Knowledge base article
Hide .html extension using .htaccess
This article will teach you how to hide the .html extension from your domain URL using a .htaccess rewrite condition
Do you want to hide the .html file extension from your URL for cosmetic purposes? This can be implemented by adding a rewrite entry in your .htaccess file.
For example, if a webpage loads via URL https://yourdomain.com/page.html, after implementing the redirect, the webpage will be served via https://yourdomain.com/page.
To complete this guide, please ensure you have SSH Access to your hosting server. Serversaurus hosting relies on key based authentication to login via SSH/SFTP. To set up SSH access you can generate a SSH key pair and configure it in cPanel before proceeding with this guide.
Let's get started!
- Open your terminal window and SSH to your hosting server:
ssh username@yourserver.serversaurus.com.au
- Change into your public_html directory (or alternative document root location):
cd public_html
- Edit the .htaccess file using vi:
vi .htaccess
- Paste the following text to the beginning of your .htaccess file by typing i and then paste the text:
RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME}.html -f RewriteRule ^(.+)$ $1.html [L,QSA] RewriteCond %{THE_REQUEST} ^[A-Z]{3,9} /.*.html HTTP/ RewriteRule ^(.*).html$ /$1 [R=301,L]
Once completed press esc then enter :wq to save your files changes.
Well done! Your URLs will now be rewritten without the HTML file extension.
Last updated November 30, 2023