Skip to content

Instantly share code, notes, and snippets.

@bepatrickdavid
Created October 16, 2019 11:59
Show Gist options
  • Save bepatrickdavid/c353e63f4e40d5af6852f3cb7eb47221 to your computer and use it in GitHub Desktop.
Save bepatrickdavid/c353e63f4e40d5af6852f3cb7eb47221 to your computer and use it in GitHub Desktop.
.htaccess: rewrite html extension and redirect to subfolder lang
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# redirect to subfolder /it if language is it
RewriteCond %{HTTP:Accept-Language} ^it [NC]
RewriteRule ^/?$ https://domain.com/it/ [R,NC,L]
# 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]
# redirect http to https set on the server panel (cpanel/plesk)
</IfModule>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment