Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Apache: Hide HTML extension in URLs with htaccess
# This tag loads the rewrite module
<IfModule mod_rewrite.c>
# enable the rewrite engine
RewriteEngine On
# Set your root directory
RewriteBase /
# Remove the .html extension
RewriteCond %{THE_REQUEST} ^GET\ (.*)\.html\ HTTP
RewriteRule (.*)\.html$ $1 [R=301]
# Remove index and reference the directory
RewriteRule (.*)/index$ $1/ [R=301]
# Remove trailing slash if not a directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /$
RewriteRule (.*)/ $1 [R=301]
# Forward request to html file, **but don't redirect (bot friendly)**
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.html [L]
</IfModule>
# ------------- alternative remove .html version, 2020 -------------------
#remove html file extension https://example.com/page.html
# to https://example.com/page
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC, L]
The search engine may index these pages as duplicate content,
to overcome this add a <canonical> meta tag in the HTML file.
Example:
<link rel="canonical" href="https://example.com/blog/first-blog" />
@luckmoshy

This comment has been minimized.

Copy link

@luckmoshy luckmoshy commented Feb 27, 2020

perfect

@Scorpovi4

This comment has been minimized.

Copy link

@Scorpovi4 Scorpovi4 commented Jan 14, 2021

Works well!

@davidvandenbor

This comment has been minimized.

Copy link
Owner Author

@davidvandenbor davidvandenbor commented Feb 1, 2021

😄 Thanks for the feedback. Nice to see there's still love for static websites! I use this piece of code for static websites rendered/made with Eleventy.

@rngala

This comment has been minimized.

Copy link

@rngala rngala commented Aug 24, 2021

Found it useful as well! I love and still use static websites. Thanks for the clear guide and share.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment