Skip to content

Instantly share code, notes, and snippets.

@davidvandenbor
Last active May 8, 2024 07:37
Show Gist options
  • Select an option

  • Save davidvandenbor/f5a2c18c472ceb68d0dd to your computer and use it in GitHub Desktop.

Select an option

Save davidvandenbor/f5a2c18c472ceb68d0dd to your computer and use it in GitHub Desktop.
Apache: Hide HTML extension in URLs with htaccess
# This tag ensures the rewrite module is loaded
<IfModule mod_rewrite.c>
# enable the rewrite engine ===== rewrite engine aanzetten
RewriteEngine On
# Set your root directory ===== leg je root directory vast
RewriteBase /
# remove the .html extension ===== verwijder de html extensie
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>
@luckmoshy
Copy link

perfect

@Scorpovi4
Copy link

Works well!

@davidvandenbor
Copy link
Author

😄 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
Copy link

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