Skip to content

Instantly share code, notes, and snippets.

@up209d
Created November 28, 2017 07:26
Show Gist options
  • Save up209d/c164d620f475f9ad616f03a0bdee5caa to your computer and use it in GitHub Desktop.
Save up209d/c164d620f475f9ad616f03a0bdee5caa to your computer and use it in GitHub Desktop.
Single Page Application .htaccess (gzip and fallback)
AddEncoding gzip .gz
RewriteEngine on
#For all request has header contents Accept-encoding: gzip
RewriteCond %{HTTP:Accept-encoding} gzip
#For all browser except Konqueror
RewriteCond %{HTTP_USER_AGENT} !Konqueror
#For all filename case, if the name.ext.gz file exits so we do rewrite
#We can use flag like [OR] [AND] for condition
RewriteCond %{REQUEST_FILENAME}.gz -f
RewriteRule ^(.*\.(css|js|html|json|svg))$ $1.gz [QSA,L]
<Files *.css.gz>
ForceType text/css
</Files>
<Files *.js.gz>
ForceType application/javascript
</Files>
<Files *.html.gz>
ForceType text/html
</Files>
<Files *.json.gz>
ForceType application/json
</Files>
<Files *.svg.gz>
ForceType image/svg+xml
</Files>
#For Fallback API:
#Point to index.html and if index.html.gz is not found
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.html [QSA,L]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment