# This line is used for IE that doesn't accept x-gzip as encoding
AddEncoding gzip .gz

# Some Apache 2.2.x server will use incorrect MIME types for compressed files
<Files *.js.gz>
  AddType "text/javascript" .gz
</Files>
<Files *.css.gz>
  AddType "text/css" .gz
</Files>
<FilesMatch "\.(html.gz|xhtml.gz|htm.gz)$">
  AddType "text/html" .gz
</FilesMatch>

##
# The cache options allow the server to return information on how file should be chached on the browser.
# This allows your site to be much faster when a user isnavigating through it or
# when s/he already visited the site.
#
# Here are some option for the Cache-Control HTTP header.
# max-age: Maximum age of a file before revalidation is needed. Not used as set by the ExpireDefault line.
# must-revalidate: The file should be revalidated each time
# private: The file should be store in the user private cache
# public: The file can be stored in a common cache
# proxy-revalidate: If the cache is a proxy server, the file should be revalidated
#
# Depending on how often you change the content of you web site, you may want
# to increase or decrease the ExpireDefault values (1 hour = A3600)
#
# Depending on the content of you web site, you may want to adapt the
# Cache-Control with public, private, proxy-revalidate or must-revalidate.
##

# Activate the cache options
ExpiresActive On

# Default files (not specified) below return a max age of 1 hour
ExpiresDefault A3600

# 1 WEEK
<FilesMatch "\.(ico|gif|jpe?g|png|flv|swf|mov|mp3|wmv|ppt|wml)$">
  FileETag All
  ExpiresDefault A604800
  Header append Cache-Control "private"
</FilesMatch>

# 1 DAY 2 HOURS
<FilesMatch "\.(xml|txt|ini|jar|pack\.gz|exe|tgz|tar\.gz|jad|jnlp)$">
  FileETag All
  ExpiresDefault A93600 
  Header append Cache-Control "public"
</FilesMatch>

# 2 HOURS
<FilesMatch "\.(html|xhtml|htm|js|css|pdf|html\.gz|xhtml\.gz|htm\.gz|js\.gz|css\.gz)$">
  FileETag All
  ExpiresDefault A7200 
  Header append Cache-Control "private, proxy-revalidate, must-revalidate"
</FilesMatch>

# NEVER CACHE
<FilesMatch "\.(php|cgi|pl)$">
  ExpiresDefault A0 
  Header set Cache-Control "no-cache, no-store, must-revalidate, max-age=0, proxy-revalidate, no-transform"
  Header set Pragma "no-cache"
</FilesMatch>

# GZ handling
RewriteEngine On

RewriteCond %{HTTP:Accept-Encoding} gzip
RewriteCond %{HTTP_USER_AGENT} !Safari
RewriteCond %{REQUEST_FILENAME}.gz -f
RewriteRule ^(.*\.html|.*\.xhtml|.*\.htm|.*\.js|.*\.css)$ $1.gz [NC,L]
