Tuesday 31 January 2023

How to remove .html from URL?

 Use the following code in .htaccess file to remove .html from url. 

This code will work for static html files. 

example.com/page.html   to example.com/page

<IfModule mod_rewrite.c>

RewriteEngine on

RewriteBase /

RewriteCond %{https://example.com} !(\.[^./]+)$

RewriteCond %{REQUEST_fileNAME} !-d

RewriteCond %{REQUEST_fileNAME} !-f

RewriteRule (.*) /$1.html [L]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^.]+)\.html\ HTTP

RewriteRule ^([^.]+)\.html$ https://example.com/$1 [R=301,L]

</IfModule>