#Fix Rewrite
Options -Multiviews

RewriteEngine on

# enable symbolic links
Options +FollowSymLinks

# Disable directory browsing
Options All -Indexes

# Set the directory index
DirectoryIndex index.php

# protect against DOS attacks by limiting file upload size [bytes]
LimitRequestBody 10240000

# God forbit an uncaught error - Display contact method
ServerSignature EMail
SetEnv SERVER_ADMIN Support@Miles.Systems

# Remove www.
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/ [R=301]

# set the default language
DefaultLanguage en-US

# pass the default character set
AddDefaultCharset utf-8

# this needs to be somewhere
# LoadModule http2_module modules/mod_http2.so
ProtocolsHonorOrder On
Protocols h2 http/1.1

# Enable compression
<ifModule mod_gzip.c>
    mod_gzip_on Yes
    mod_gzip_dechunk Yes
    mod_gzip_item_include file .(html?|txt|css|js|php|pl)$
    mod_gzip_item_include handler ^cgi-script$
    mod_gzip_item_include mime ^text/.*
    mod_gzip_item_include mime ^application/x-javascript.*
    mod_gzip_item_exclude mime ^image/.*
    mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
</ifModule>

<IfModule mod_speling.c>
	CheckSpelling On
</IfModule>

# Error Redirects
ErrorDocument 404 https://carbonphp.com/404/

# Version Control for Dependancies
RewriteRule ^(.*)\.[\d]{10}\.(css|js|html)$ $1.$2 [L]

#prevent hotlinking
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^https://carbonphp.com/.*$ [NC]
RewriteRule \.(ico|pdf|flv|jpg|jpeg|png|gif|swf|xml|txt|css|html|htm|php|hbs|js)$ - [F]

<FilesMatch "\.(ico|pdf|flv)$"> # 1 YEAR - 29030400; 1 WEEK - 604800; 2 DAYS - 172800; 1 MIN  - 60
    Header set Cache-Control "max-age=29030400, public"
</FilesMatch>

<FilesMatch "\.(jpg|jpeg|png|gif|swf|xml|txt|css)$">
    Header set Cache-Control "max-age=604800, public"
</FilesMatch>

<FilesMatch "\.(html|htm|php|hbs|js)$"> # TODO - Eventually we should cache mustache files
    Header set Cache-Control "max-age=0, private, public" # normally 60
</FilesMatch>


# deny access to evil robots site rippers offline browsers and other nasty scum
RewriteBase /
RewriteCond %{HTTP_USER_AGENT} ^Anarchie [OR]
RewriteCond %{HTTP_USER_AGENT} ^ASPSeek [OR]
RewriteCond %{HTTP_USER_AGENT} ^attach [OR]
RewriteCond %{HTTP_USER_AGENT} ^autoemailspider [OR]
RewriteCond %{HTTP_USER_AGENT} ^Xaldon\ WebSpider [OR]
RewriteCond %{HTTP_USER_AGENT} ^Xenu [OR]
RewriteCond %{HTTP_USER_AGENT} ^Zeus.*Webster [OR]
RewriteCond %{HTTP_USER_AGENT} ^Zeus
RewriteRule ^.*$ http://www.cnn.com [R,L]
# send em to a hellish website of your choice

# https://serverfault.com/questions/940923/apache2-with-http-2-serves-some-content-with-h2-some-with-http-1-1
RewriteCond %{HTTP:Upgrade} websocket               [NC]
RewriteRule /(.*)           wss://localhost:8004/$1  [P]


# Redirect anything that's not of the following file types to the index
RewriteCond %{REQUEST_URI} !(\.png|\.jpg|\.gif|\.svg|\.json|\.jpeg|\.bmp|\.icon|\.js|\.css|\.woff|.\woff2|\.map|\.hbs)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d   -- We dont want to redirect into direcotries
RewriteRule ^(.*)$ /index.php [L,QSA]
