Skip to content

Instantly share code, notes, and snippets.

@lomboboo
Created November 1, 2017 12:59
Show Gist options
  • Save lomboboo/396aa42762cd1f5f388457d6730d9186 to your computer and use it in GitHub Desktop.
Save lomboboo/396aa42762cd1f5f388457d6730d9186 to your computer and use it in GitHub Desktop.
htaccess file - dynamic (subdirectories) removing html extension, redirecting filename.html to filename
RewriteEngine On
RewriteBase /
# %{ENV:BASE} is now subdirectory, ex. /folder/subfolder/
RewriteCond %{REQUEST_URI}::$1 ^(.*?/)(.*)::\2$
RewriteRule ^(.*)$ - [E=BASE:%1]
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{THE_REQUEST} /(.+?)\.html[\s?] [NC]
RewriteRule ^ %1 [R=302,L,NE]
# rewrite index to /
RewriteRule ^(.*)index$ %{ENV:BASE} [R=301,NC,L]
# To internally forward /dir/file to /dir/file.html
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}\.html -f [NC]
RewriteRule ^(.+?)/?$ %{ENV:BASE}$1.html [L]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment