# ##############################################################################
# # SECURITY                                                                   #
# ##############################################################################

# ------------------------------------------------------------------------------
# | Disable indexes                                                            |
# ------------------------------------------------------------------------------

Options -Indexes

# ------------------------------------------------------------------------------
# | File access                                                                |
# ------------------------------------------------------------------------------

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Hidden files/folders (.git, .gitignore, .svn etc)

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_URI} "!(^|/)\.well-known/([^./]+./?)+$" [NC]
    RewriteCond %{SCRIPT_FILENAME} -d [OR]
    RewriteCond %{SCRIPT_FILENAME} -f
    RewriteRule "(^|/)\." - [F]
</IfModule>

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Sensitive filetypes

<FilesMatch "(^#.*#|\.(bak|codekit3|config|dist|fla|inc|ini|log|psd|sh|sql|ss|sublime-.*|sw[op]|ya?ml)|~)$">
    # Apache < 2.3
    <IfModule !mod_authz_core.c>
        Order allow,deny
        Deny from all
        Satisfy All
    </IfModule>

    # Apache ≥ 2.3
    <IfModule mod_authz_core.c>
        Require all denied
    </IfModule>
</FilesMatch>

# ------------------------------------------------------------------------------
# | Headers/proxies                                                            |
# ------------------------------------------------------------------------------

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Clean unused proxy headers

<IfModule mod_headers.c>
    RequestHeader unset X-Forwarded-Host
</IfModule>

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Ensures that X-Forwarded-Host can only be used to determine the request host
# name if the proxy's IP address is explicitly trusted

<IfModule mod_env.c>
    SetEnv BlockUntrustedIPs true
</IfModule>

# ##############################################################################
# # URL REWRITES                                                               #
# ##############################################################################

# ------------------------------------------------------------------------------
# | Rewrite rules                                                              |
# ------------------------------------------------------------------------------

<IfModule mod_rewrite.c>
    <IfModule mod_env.c>
        SetEnv HTTP_MOD_REWRITE On
    </IfModule>

    RewriteEngine On

    # Force www.actualdomain.com with 301 redirects
    # RewriteCond %{HTTP_HOST} !^.*\.(test|dev|devdomain\.co\.uk)$ [NC]
    # RewriteCond %{HTTP_HOST} !^www\.actualdomain\.com$ [NC]
    # RewriteRule ^(.*)$ http://www.actualdomain.com/$1 [R=301,L]

    RewriteBase '/'

    # Force trailing slash
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_METHOD} =GET
    RewriteCond %{REQUEST_URI} !^/admin/
    RewriteCond %{REQUEST_URI} !^/sitemap\.xml
    RewriteRule ^(.*)([^/])$ $1$2/ [R=301,NE,L]

    # SilverStripe rewrites - includes optional maintenance rule for the above
    RewriteCond %{REQUEST_URI} ^(.*)$
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule .* index.php
</IfModule>

# ------------------------------------------------------------------------------
# | Error documents                                                            |
# ------------------------------------------------------------------------------

ErrorDocument 404 /assets/error-404.html
ErrorDocument 500 /assets/error-500.html

# ##############################################################################
# # INTERNET EXPLORER                                                          #
# ##############################################################################

# ------------------------------------------------------------------------------
# | Force IE to use latest version                                             |
# ------------------------------------------------------------------------------

<IfModule mod_headers.c>
    Header set X-UA-Compatible "IE=Edge,chrome=1"
    # mod_headers can't match by content-type, but we don't want to send this header on *everything*
    <FilesMatch "\.(js|css|gif|png|jpe?g|pdf|xml|oga|ogg|m4a|ogv|mp4|m4v|webm|svg|svgz|eot|ttf|otf|woff|ico|webp|appcache|manifest|htc|crx|xpi|safariextz|vcf)$" >
        Header unset X-UA-Compatible
    </FilesMatch>
</IfModule>

# ##############################################################################
# # COMPRESSION, MIME TYPES, ENCODING                                          #
# ##############################################################################

# ------------------------------------------------------------------------------
# | Web fonts access                                                           |
# ------------------------------------------------------------------------------

# Allow access from all domains for web fonts

<IfModule mod_headers.c>
    <FilesMatch "\.(eot|otf|ttc|ttf|woff)$">
        Header set Access-Control-Allow-Origin "*"
    </FilesMatch>
</IfModule>

# ------------------------------------------------------------------------------
# | Gzip compression                                                           |
# ------------------------------------------------------------------------------

<IfModule mod_deflate.c>

    # Force compression for mangled headers.
    # http://developer.yahoo.com/blogs/ydn/posts/2010/12/pushing-beyond-gzipping
    <IfModule mod_setenvif.c>
        <IfModule mod_headers.c>
            SetEnvIfNoCase ^(Accept-EncodXng|X-cept-Encoding|X{15}|~{15}|-{15})$ ^((gzip|deflate)\s*,?\s*)+|[X~-]{4,13}$ HAVE_Accept-Encoding
            RequestHeader append Accept-Encoding "gzip,deflate" env=HAVE_Accept-Encoding
        </IfModule>
    </IfModule>

    # HTML, TXT, CSS, JavaScript, JSON, XML, HTC:
	<IfModule mod_filter.c>
        AddOutputFilterByType DEFLATE application/atom+xml \
                                      application/javascript \
                                      application/json \
                                      application/ld+json \
                                      application/manifest+json \
                                      application/rss+xml \
                                      application/vnd.ms-fontobject \
                                      application/x-font-ttf \
                                      application/x-web-app-manifest+json \
                                      application/xhtml+xml \
                                      application/xml \
                                      font/opentype \
                                      image/svg+xml \
                                      image/x-icon \
                                      text/css \
                                      text/html \
                                      text/plain \
                                      text/vtt \
                                      text/x-component \
                                      text/xml
    </IfModule>

    <IfModule !mod_filter.c>
        # Legacy versions of Apache
        AddOutputFilterByType DEFLATE text/html text/plain text/css application/json
        AddOutputFilterByType DEFLATE application/javascript
        AddOutputFilterByType DEFLATE text/xml application/xml text/x-component
        AddOutputFilterByType DEFLATE application/xhtml+xml application/rss+xml application/atom+xml
        AddOutputFilterByType DEFLATE image/x-icon image/svg+xml application/vnd.ms-fontobject application/x-font-ttf font/opentype
    </IfModule>

</IfModule>

# ------------------------------------------------------------------------------
# | Proper MIME types for all files                                            |
# ------------------------------------------------------------------------------

<IfModule mod_mime.c>

# Audio
    AddType audio/mp4                                   m4a f4a f4b
    AddType audio/ogg                                   oga ogg

# JavaScript
    # Normalize to standard type (it's sniffed in IE anyways):
    # http://tools.ietf.org/html/rfc4329#section-7.2
    AddType application/javascript                      js
    AddType application/json                            json

# Video
    AddType video/mp4                                   f4v f4p m4v mp4
    AddType video/ogg                                   ogv
    AddType video/webm                                  webm
    AddType video/x-flv                                 flv

# Web fonts
    AddType application/font-woff                       woff
    AddType application/vnd.ms-fontobject               eot

    # Browsers usually ignore the font MIME types and sniff the content,
    # however, Chrome shows a warning if other MIME types are used for the
    # following fonts.
    AddType application/x-font-ttf                      ttc ttf
    AddType font/opentype                               otf

    # Make SVGZ fonts work on iPad:
    # https://twitter.com/FontSquirrel/status/14855840545
    AddType     image/svg+xml                           svgz
    AddEncoding gzip                                    svgz

# Other
    AddType application/octet-stream                    safariextz
    AddType application/x-chrome-extension              crx
    AddType application/x-opera-extension               oex
    AddType application/x-web-app-manifest+json         webapp
    AddType application/x-xpinstall                     xpi
    AddType application/xml                             atom rdf rss xml
    AddType image/webp                                  webp
    AddType image/x-icon                                cur
    AddType text/cache-manifest                         appcache manifest
    AddType text/vtt                                    vtt
    AddType text/x-component                            htc
    AddType text/x-vcard                                vcf

</IfModule>

# ------------------------------------------------------------------------------
# | UTF-8 encoding                                                             |
# ------------------------------------------------------------------------------

# Use UTF-8 encoding for anything served as `text/html` or `text/plain`.
AddDefaultCharset utf-8

# Force UTF-8 for certain file formats.
<IfModule mod_mime.c>
    AddCharset utf-8 .atom .css .js .json .rss .vtt .webapp .xml
</IfModule>

# ##############################################################################
# # CACHE CONTROL                                                              #
# ##############################################################################

# ------------------------------------------------------------------------------
# | ETag removal                                                               |
# ------------------------------------------------------------------------------

# `FileETag None` is not enough for every server.
<IfModule mod_headers.c>
    Header unset ETag
</IfModule>

FileETag None

# ------------------------------------------------------------------------------
# | Expires headers (for better cache control)                                 |
# ------------------------------------------------------------------------------

<IfModule mod_expires.c>
    ExpiresActive on

# Perhaps better to whitelist expires rules? Perhaps.
    ExpiresDefault                                      "access plus 1 month"

# Manifest files
    ExpiresByType text/cache-manifest                   "access plus 0 seconds"
    ExpiresByType application/x-web-app-manifest+json   "access plus 0 seconds"

# HTML
    ExpiresByType text/html                             "access plus 0 seconds"

# Data
    ExpiresByType text/xml                              "access plus 0 seconds"
    ExpiresByType application/xml                       "access plus 0 seconds"
    ExpiresByType application/json                      "access plus 0 seconds"

# Feed
    ExpiresByType application/rss+xml                   "access plus 1 hour"
    ExpiresByType application/atom+xml                  "access plus 1 hour"

# Favicon (cannot be renamed)
    ExpiresByType image/x-icon                          "access plus 1 week"

# Media: images, video, audio
    ExpiresByType image/gif                             "access plus 1 year"
    ExpiresByType image/png                             "access plus 1 year"
    ExpiresByType image/jpg                             "access plus 1 year"
    ExpiresByType image/jpeg                            "access plus 1 year"
    ExpiresByType video/ogg                             "access plus 1 year"
    ExpiresByType audio/ogg                             "access plus 1 year"
    ExpiresByType video/mp4                             "access plus 1 year"
    ExpiresByType video/webm                            "access plus 1 year"
    ExpiresByType image/svg+xml                         "access plus 1 year"

# HTC files  (css3pie)
    ExpiresByType text/x-component                      "access plus 1 month"

# Webfonts
    ExpiresByType application/x-font-ttf                "access plus 1 month"
    ExpiresByType font/opentype                         "access plus 1 month"
    ExpiresByType application/x-font-woff               "access plus 1 month"
    ExpiresByType application/vnd.ms-fontobject         "access plus 1 month"

# CSS and JavaScript
    ExpiresByType text/css                              "access plus 1 year"
    ExpiresByType application/javascript                "access plus 1 year"

</IfModule>
