Skip to content

Instantly share code, notes, and snippets.

@azharisubroto
Forked from jentanbernardus/.htaccess
Created June 10, 2018 16:41
Show Gist options
  • Save azharisubroto/653ad97fb715b40c632d2aed2d874b4a to your computer and use it in GitHub Desktop.
Save azharisubroto/653ad97fb715b40c632d2aed2d874b4a to your computer and use it in GitHub Desktop.
.htaccess Rules for Better Google Page Speed Results - Here's a set of default rules that I add to any site that I want to significantly increase the speed of. It took a lot of weeding through and testing, but this seems to work the best and will (guarantee not included) increase your Google Page Speed score by taking care of a lot of the cache …
# Turn on Expires and set default to 0
ExpiresActive On
ExpiresDefault A0
# Set up caching on media files for 1 year (forever?)
<FilesMatch "\.(flv|ico|pdf|avi|mov|ppt|doc|mp3|wmv|wav)$">
ExpiresDefault A29030400
Header append Cache-Control "public"
</FilesMatch>
# Set up caching on media files for 1 week
<FilesMatch "\.(gif|jpg|jpeg|png|swf)$">
ExpiresDefault A604800
Header append Cache-Control "public"
</FilesMatch>
# Set up 2 Hour caching on commonly updated files
<FilesMatch "\.(xml|txt|js|css)$">
ExpiresDefault A604800
Header append Cache-Control "proxy-revalidate"
</FilesMatch>
<FilesMatch "\.(js|css|xml|gz)$">
Header append Vary Accept-Encoding
</FilesMatch>
# Force no caching for dynamic files
<FilesMatch "\.(php|cgi|pl|htm)$">
ExpiresActive Off
Header set Cache-Control "private, no-cache, no-store, proxy-revalidate, no-transform"
Header set Pragma "no-cache"
</FilesMatch>
AddEncoding gzip .gz
<Files *.css.gz>
ForceType text/css
</Files>
<Files *.js.gz>
ForceType text/javascript
</Files>
<FilesMatch .*\.(js|css)>
RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} !MSIE
RewriteCond %{HTTP:Accept-encoding} gzip
RewriteCond %{REQUEST_FILENAME}.gz -f
RewriteRule ^(.*)$ $1.gz [L]
</FilesMatch>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment