RewriteEngine	on

DirectorySlash	off			# personal preference, given my site nav--not recommended to copy
DirectoryIndex	index.html

# error messages:
ErrorDocument 404 /404

# do not mess with the subdomains
# (I’ve named them with folders starting with “_”)
RewriteRule ^_ - [L]

# this ‘feature’ (MultiViews) drove me absolutely insane!
# without this, Apache will give a 404 for a rewrite if a folder of the same name does not exist (e.g. “/blog/hello”)
# <webmasterworld.com/apache/3808792.htm>
Options -MultiViews +Indexes

# allow you guys to see this file
<Files .htaccess>
	allow from all
</Files>

# no www. also, I don’t know why, but there seems to be incoming links using wap.*, m.* &c. for no reason (spambots?)
RewriteCond %{HTTP_HOST} ^(?:www?|wap|m|mobile|macmini|fake)\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

# leave my google verification alone
RewriteRule ^google.*\.html$ - [L]


# additional filetypes
# --------------------------------------------------------------------------------------------------------------------------
AddType video/ogg		.ogv
AddType audio/ogg		.oga .ogg
AddType video/mp4		.mp4
AddType video/webm		.webm

AddType font/ttf		.ttf
AddType font/otf		.otf
AddType font/x-woff		.woff
AddType image/svg+xml		.svg .svgz
AddEncoding gzip .svgz

AddType application/rss+xml	.rss
AddType text/remarkable		.rem
AddType text/plain		.do	# “to.do” files

<IfModule mod_deflate.c>
	AddOutputFilterByType DEFLATE text/html text/plain text/css application/x-javascript text/javascript application/javascript application/json text/xml application/xml application/rss+xml
	
	<FilesMatch "\.(ttf|otf|svg)$">
		SetOutputFilter DEFLATE
	</FilesMatch>
</IfModule>

# force utf-8 for a number of file formats:
AddDefaultCharset utf-8
AddCharset utf-8 .rem .html .css .rss .xml .js .txt .do

# caching
# --------------------------------------------------------------------------------------------------------------------------
FileETag MTime Size
<IfModule mod_expires.c>
	Header set cache-control: public
	ExpiresActive on
	ExpiresDefault "access plus 1 month"
	ExpiresByType text/html			"access plus 1 hour"
	ExpiresByType application/rss+xml	"access plus 1 hour"
	ExpiresByType image/png			"access plus 1 month"
	ExpiresByType image/jpg			"access plus 1 month"
	ExpiresByType image/jpeg		"access plus 1 month"
	ExpiresByType video/ogg			"access plus 1 month"
	ExpiresByType audio/ogg			"access plus 1 month"
	ExpiresByType video/mp4			"access plus 1 month"
	ExpiresByType video/webm		"access plus 1 month"
	ExpiresByType image/x-icon		"access plus 1 year"
	ExpiresByType font/ttf			"access plus 1 year"
	ExpiresByType image/svg+xml		"access plus 1 year"
</IfModule>

# # prevent 3G carriers and proxies from breaking my code with their incompetent compression methods
# # (this currently 500s and stuff needs to be modified for the /design/ rewrite)
# <Files ~ "\.(cs[sz]|rs[sz]|html5)$">
# 	Header merge Cache-Control no-transform
# </Files>



# to seperate content / code webroot is pure content, and the site’s code / design is all within ‘/.system/’
RewriteRule ^robots.txt$  /.system/robots.txt [NC,L]
RewriteRule ^favicon.ico$ /.system/design/favicon.ico [NC,L]
RewriteRule ^apple-touch-icon.png$ /.system/design/apple-touch-icon.png [L]
RewriteRule ^sitemap.xml$ /.system/sitemap.xml [L]


# ==========================================================================================================================
# ==========================================================================================================================
# legacy v0.1 URL redirector
RewriteCond %{QUERY_STRING} .+
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^ /.system/archive/legacy.php [L]
# ==========================================================================================================================


RewriteCond %{REQUEST_URI} ^/design/?$
RewriteRule ^design/$ /.system/design/design.css [L]
RewriteRule ^design/(.*)$ /.system/design/$1 [L]

## Temporary hijack the RSS feeds of other categories to point toward the main feed until the $2K has been raised for Clint
##RewriteRule ^[a-z0-9-]+/rss$ /rss [R=302,L]

# give the compressed RSS file for the given directory
# if the file exists in the cache use that, if not, pass to the PHP to generate
RewriteCond %{REQUEST_URI} ^/([a-z0-9-]+/)?rss$
RewriteCond %{DOCUMENT_ROOT}/.cache/%1feed.rss -F
RewriteRule ^([a-z0-9-]+/)?rss$ /.cache/$1feed.rss [L]
RewriteRule ^(?:([a-z0-9-]+)/)?rss$ /.system/rss.php?category=$1 [L]


# view-source
# --------------------------------------------------------------------------------------------------------------------------
# you can view the source of all the PHP files on the site due to the distinction that no PHP files are called directly by
# name in the site, instead all PHP is executed via some kind of RewriteRule. therefore we make it that any direct visit to
# a PHP file shows in plain text, but rewrites execute the PHP instead
RewriteCond %{THE_REQUEST} \.(htaccess|php|rem|sh)(\?.*)?\ HTTP
RewriteRule ^.*(?<!view-source\.php)$ /.system/view-source.php?file=$0 [L]


# article content
# --------------------------------------------------------------------------------------------------------------------------
## Temporary hijack whilst I’m away on holiday
##RewriteCond %{REQUEST_URI} ^/(?:[a-z0-9-]+/)?[a-z0-9_-]+(\.html)?$
##RewriteCond %{REQUEST_URI} ^/(?:[a-z0-9-]+/)?(?!help_clint|quantumsandbox_1|kroc_writes|tour_de_england)[a-z0-9_-]+(\.html)?$
##RewriteRule ^(?:[a-z0-9-]+/)?[a-z0-9_-]+(\.html)?$ /help_clint$1 [R=302,L]

# requesting a tag page (e.g. “/blog/”) - show the latest article
# rewrite to “…/latest”, which will be checked against cache below, and processed accrodingly by article.php
RewriteRule ^([a-z0-9-]+/)?$ /$1latest [PT]

#editing an article?
##RewriteRule ^((?:[a-z0-9-]+/)?[a-z0-9_-]+):edit$ /.system/edit.php?article=$1.rem [L]

# requesting an article? (e.g. “/blog/hello”)
# if the file is not in the cache (or we are viewing the HTML), run it through the PHP to generate the article
RewriteCond %{REQUEST_URI} ^(/(?:[a-z0-9-]+/)?[a-z0-9_-]+)(\.html)?$
RewriteCond %{THE_REQUEST} \b.*?\.html\b [OR]
RewriteCond %{DOCUMENT_ROOT}/.cache%1.html !-F
RewriteRule ^((?:[a-z0-9-]+/)?[a-z0-9_-]+)(\.html)?$ /.system/article.php?article=$1.rem [L]
# …otherwise load the cache
RewriteRule ^((?:[a-z0-9-]+/)?[a-z0-9_-]+)(\.html)?$ /.cache/$1.html [L]


# directory browsing (under construction)
# --------------------------------------------------------------------------------------------------------------------------
RewriteRule ^(?:(\.(?!\.)|[^.])+\/)*$ /.system/dir.php?path=$0 [L]


# stop hotlinking
# --------------------------------------------------------------------------------------------------------------------------
# outright block access to the following files
# (too many people getting Video for Everybody wrong and hotlinking my files)
# need to optimise this with env variables or something to reduce repetition
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^https?://(fever\..*|([a-z0-9]+\.)?(malgadey|zwonull|relativwenigbartwuchs|google|yahoo|bloglines)((\.[a-z]+){1,2})?|.*/fever)/ [NC]
RewriteRule \.(swf|og[agv]|mp4|m4v|ttf|ttz|svg|svz)$ - [F,L]

# don’t get stuck in a loop
RewriteRule ^\.cache/ - [L]

# allowed domains
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^https?://(fever\..*|([a-z0-9]+\.)?(malgadey|zwonull|relativwenigbartwuchs|google|yahoo|bloglines)((\.[a-z]+){1,2})?|.*/fever)/ [NC]
RewriteCond %{REQUEST_URI} ^.*(?<!_preview)\.(jpe?g|png)$
RewriteCond %{DOCUMENT_ROOT}/.cache%0 -F
RewriteRule ^.*(?<!_preview)\.(jpe?g|png)$ /.cache/$0 [L]

RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^https?://(fever\..*|([a-z0-9]+\.)?(malgadey|zwonull|relativwenigbartwuchs|google|yahoo|bloglines)((\.[a-z]+){1,2})?|.*/fever)/ [NC]
RewriteRule ^.*(?<!_preview)\.(jpe?g|png)$ /.system/hotlink.php?image=$0 [L]