Skip to content

Instantly share code, notes, and snippets.

@moritzebeling
Last active February 9, 2020 17:35
Show Gist options
  • Save moritzebeling/5c17d13208a620cf4e0bd4933b304b70 to your computer and use it in GitHub Desktop.
Save moritzebeling/5c17d13208a620cf4e0bd4933b304b70 to your computer and use it in GitHub Desktop.
htaccess boilerplate
### charset
AddDefaultCharset UTF-8
### directory index
DirectoryIndex index.html
### avoid directory listing
Options -Indexes
### default language
DefaultLanguage en
### add file types (https://gist.github.com/asakasinsky/6126409)
<IfModule mod_mime.c>
AddType text/html .html
AddType application/json .json
# AddType text/plain .txt
# AddType text/markdown .md
# AddType application/xml .xml
AddType text/css .css
AddType text/javascript .js
AddType image/jpeg .jpeg .jpg .JPG
AddType image/png .png
AddType image/svg+xml .svg
AddType image/x-icon .ico
# AddType image/tiff .tiff .tif
# AddType image/gif .gif
AddType application/x-font-woff .woff
AddType application/x-font-woff .woff2
# AddType font/truetype .ttf
# AddType font/opentype .otf
# AddType font/eot .eot
# AddType application/zip .zip
# AddType application/pdf .pdf
# AddType application/postscript .ai .eps
# AddType application/unityweb .unityweb
# AddType audio/mpeg .mp3
# AddType audio/x-wav .wav
AddType video/mp4 .mp4
# AddType video/webm .webm
# AddType video/ogg .ogv
# AddType video/quicktime .mov .qt
</IfModule>
### allow a remote host to reference files (CORS)
<IfModule mod_headers.c>
# Header Set Access-Control-Allow-Origin "http://localhost"
</IfModule>
### enable keep alive
<ifModule mod_headers.c>
# Header set Connection keep-alive
</ifModule>
### compress text file responses
<IfModule mod_deflate.c>
<FilesMatch "\.(txt|html|md|css|js|json|xml)$" >
SetOutputFilter DEFLATE
</FilesMatch>
<FilesMatch "\.(woff|woff2|ttf|otf|eot|svg)$" >
SetOutputFilter DEFLATE
</FilesMatch>
</IfModule>
### set browser caching
<IfModule mod_expires.c>
ExpiresActive On
# ExpiresByType text/html "access 1 day"
ExpiresByType application/json "access 1 day"
# ExpiresByType text/plain "access 1 day"
# ExpiresByType text/markdown "access 1 day"
# ExpiresByType application/xml "access 1 day"
ExpiresByType text/css "access 1 month"
ExpiresByType text/javascript "access 1 month"
ExpiresByType image/jpeg "access 1 year"
ExpiresByType image/png "access 1 year"
ExpiresByType image/svg+xml "access 1 year"
ExpiresByType image/x-icon "access 1 year"
# ExpiresByType image/tiff "access 1 year"
# ExpiresByType image/gif "access 1 year"
ExpiresByType application/x-font-woff "access 1 year"
# ExpiresByType font/truetype "access 1 year"
# ExpiresByType font/opentype "access 1 year"
# ExpiresByType font/eot "access 1 year"
# ExpiresByType application/zip "access 1 year"
# ExpiresByType application/pdf "access 1 year"
# ExpiresByType application/postscript "access 1 year"
# ExpiresByType application/unityweb "access 1 year"
# ExpiresByType audio/mpeg "access 1 year"
# ExpiresByType audio/x-wav "access 1 year"
ExpiresByType video/mp4 "access 1 year"
# ExpiresByType video/webm "access 1 year"
# ExpiresByType video/ogg "access 1 year"
# ExpiresByType video/quicktime "access 1 year"
ExpiresDefault "access 1 month"
</IfModule>
### rewrite
<IfModule mod_rewrite.c>
RewriteEngine on
# some servers require this
# RewriteBase /
# remove www and add https
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]
# force https
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# redirect favicon, sitemap, robots, ....
# RewriteRule ^favicon\.ico$ /path/2/favicon/favicon.ico [L]
# RewriteRule ^sitemap\.xml$ /path/2/sitemap/sitemap.xml [L]
# RewriteRule ^robots\.txt$ /path/2/robots/robots.txt [L]
</IfModule>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment