How to hide the .html extension with Apache mod_rewrite How to hide the .html extension with Apache mod_rewrite apache apache

How to hide the .html extension with Apache mod_rewrite


Try this rule:

RewriteCond %{REQUEST_FILENAME}.html -fRewriteRule !.*\.html$ %{REQUEST_FILENAME}.html [L]

This will rewrite all requests that can be mapped to an existing file when appending a .html.


The previous answers don't check if the requested path is a directory.

Here is the full rewrite condition which doesn't rewrite, if requested path is a directory (as stated by the original question):

RewriteEngine onRewriteCond %{REQUEST_FILENAME} !-d          # is not directoryRewriteCond %{REQUEST_FILENAME}\.html -f     # is an existing html fileRewriteRule ^(.*)$ $1.html                   # rewrite index to index.html

To be SEO friendly and avoid double content, redirect the .html urls:

# Redirects domain.com/file.html to domain.com/fileRewriteCond %{REQUEST_FILENAME} !-d          # is not directoryRewriteCond %{REQUEST_FILENAME}\.html -f     # is an existing html fileRewriteCond %{REQUEST_URI} ^(.+)\.html$      # request URI ends with .htmlRewriteRule (.*)\.html$ /$1 [R=301,L]        # redirect from index.html to index

If you need the same for scripts take a look here:How can I use .htaccess to hide .php URL extensions?


The accepted solution do not works when the website is configured with a virtual host / document root.

There is the solution I used:

RewriteEngine onRewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME}.html -fRewriteRule !.*\.html$ %{REQUEST_FILENAME}.html [L]