# tileserver.php integration with Apache via .htaccess

#check htaccess functionality
DirectoryIndex tileserver.php

RewriteEngine on

# Option: some hostings require RewriteBase e.g. 1&1.com
#RewriteBase /
#RewriteBase /directory/

# Option: some hostings require -MultiViews e.g. 1&1.com
#Options -MultiViews
 
# Option: Restrictions for data crawlers
#Options -Indexes

# Option: CORS header for cross-domain origin access to all data
#<ifModule mod_headers.c>
#  Header set Access-Control-Allow-Origin *
#</ifModule>

# Block direct downloading of .mbtiles
<FilesMatch "\.mbtiles$">
 Order Allow,Deny
 Deny from all
</FilesMatch>

# Mapping of the WMTS standardized URLs to real files and XML capabilities to tileserver.php

# WMTS RESTful
# ------------
# The file can be accessed directly:
# Example: http://www.tileserver.com/grandcanyon/10/192/401.png

## map /wmts/layer/[ANYTHING]z/x/y.ext -> /layer/z/x/y.ext (WMTS ignoring tilematrixset,style,..)
## BTW This is used only by Gaia which ignores the <ResourceURL template="...">
#RewriteRule ^wmts/([\w\d\._-]+)/.+?(\d+)/(\d+)/(\d+)\.(\w+)$ $1/$2/$4/$3.$5 [N]
## Example: http://www.tileserver.com/wmts/grandcanyon/style/tilematrixset/10/192/401.png
#
## map /layer/[ANYTHING]z/x/y.ext -> /layer/z/x/y.ext (WMTS ignoring tilematrix,style,..)
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteRule ^([\w\d\._-]+)/.+?(\d+)/(\d+)/(\d+)\.(\w+)$ $1/$2/$3/$4.$5 [N]
## Example: http://www.tileserver.com/grandcanyon/style/tilematrixset/10/192/401.png

## WMTS KVP
## --------
## map the /?key=value&... -> /layer/z/x/y.ext KVP getTile queries directly to files
#
## format passed as mime-extension, cleaning formats (jpeg->jpg)
#RewriteCond %{QUERY_STRING} ^(.*)format=image/jpeg(.*)$ [NC]
#RewriteRule ^(.*)$ $1?%1format=jpg%2 [N]
#RewriteCond %{QUERY_STRING} ^(.*)format=jpeg(.*)$ [NC]
#RewriteRule ^(.*)$ $1?%1format=jpg%2 [N]
#RewriteCond %{QUERY_STRING} ^(.*)format=image/png(.*)$ [NC]
#RewriteRule ^(.*)$ $1?%1format=png%2 [N]
#
## variable order of keys: TODO: sort the same way as mime-extension to fixed order
#RewriteCond %{QUERY_STRING} ^.*request=gettile.*layer=([\w\d\._-]+).*tilematrix=(\d+).*tilerow=(\d+).*tilecol=(\d+).*format=(\w+).*$ [NC]
#RewriteRule ^ %1/%2/%3/%4.%5 [N]
#RewriteCond %{QUERY_STRING} ^.*request=gettile.*layer=([\w\d\._-]+).*format=(\w+).*tilematrix=(\d+).*tilerow=(\d+).*tilecol=(\d+).*$ [NC]
#RewriteRule ^ %1/%3/%5/%4.%2 [N]
#RewriteCond %{QUERY_STRING} ^.*request=gettile.*layer=([\w\d\._-]+).*tilematrix=(\d+).*tilecol=(\d+).*tilerow=(\d+).*format=(\w+).*$ [NC]
#RewriteRule ^ %1/%2/%4/%3.%5 [N]
## Example: http://www.tileserver.com/wmts?request=getTile&layer=grandcanyon&tileMatrix=10&tilerow=192&tilecol=401&format=png
## Example: http://www.tileserver.com/wmts?service=WMTS&request=GetTile&version=1.0.0&layer=ne2geo&style=&format=image/jpeg&TileMatrixSet=WGS84&TileMatrix=1&TileRow=2&TileCol=2

# rewrite .jpeg -> .jpg
RewriteRule ^(.+).jpeg$ $1.jpg [L]

# Not modified HTTP 302
RewriteRule .* - [E=HTTP_IF_MODIFIED_SINCE:%{HTTP:If-Modified-Since}]
RewriteRule .* - [E=HTTP_IF_NONE_MATCH:%{HTTP:If-None-Match}]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(tileserver\.php)
RewriteRule ^(.*)$ tileserver.php?/$1 [L,QSA]
