ReWrite rule to add .html extension

The answer from @david-wolever redirects everything that does not end in .html (or the root) to the same URL with an added .html extension, meaning it appends a .html extension to things like CSS and JavaScripts files, e.g. it will redirect /style.css to /style.css.html which is not likely what you want. It also has the spaces after ! character which will likely caused @greggles 500s

This redirects URLs which do not end in a dot followed by 3 or 4 alphanumeric characters:

RewriteEngine On
RewriteCond %{REQUEST_URI} !\.[a-zA-Z0-9]{3,4}
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^(.*)$ $1.html

Or for finer grain control, whitelist the extensions you do not want .html appended to e.g.

RewriteCond %{REQUEST_URI} !\.(html|css|js|less|jpg|png|gif)$

Leave a Comment