Order Deny,Allow
RewriteEngine On
RewriteBase /
<FilesMatch "(phh|inc|bak|~)$">
  Deny from All
</FilesMatch>

<IfModule security2_module>
  # Cannot put modsecurity stuff in .htaccess!  If you do, 
  # everything will be blocked.
  # 
</IfModule>

GeoIPEnable On
#GeoIPDBFile /usr/share/GeoIP/GeoIP.dat
#GeoIPDBFile /usr/share/GeoIP/GeoIPv6.dat

RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^DE$
RewriteRule ^(.*)$ http://block.pglaf.org/germany.shtml [L]
# Bad syntax:
# RewriteRule ^(.*)$ %{SERVER_PROTOOL}://block.pglaf.org/germany.shtml [L]



SetEnvIf Request_URI "\.(zip|ZIP|iso|ISO|mpg|MPG|mpeg|MPEG)$" piped
SetEnvIf Remote_Addr "^127.0.0.1$" nokeepalive
SetEnvIf Request_Method "HEAD" nokeepalive
SetEnvIf piped .* nokeepalive

# deep links
RewriteCond %{HTTP_USER_AGENT} ^Mozilla/
RewriteCond %{HTTP_USER_AGENT} !Kindle
RewriteCond %{HTTP_USER_AGENT} !(NOOK|BNTV[46]00)
RewriteCond %{HTTP_COOKIE} !session_id=
# RewriteCond %{HTTP_REFERER} !^$
## gbn: RewriteCond %{HTTP_REFERER} !^http://(www\.|m\.|edit\.)?gutenberg\.org [NC]
RewriteCond %{HTTP_REFERER} !^https?://www\.google\. [NC]
RewriteCond %{HTTP_REFERER} !^https?://www\.bing\.com [NC]
RewriteCond %{HTTP_REFERER} !^http://www\.gutenberg\.net\.au [NC]
RewriteCond %{HTTP_REFERER} !^http://www\.pgcc\.net [NC]
RewriteCond %{HTTP_REFERER} !^http://dp\.rastko\.net [NC]
RewriteCond %{HTTP_REFERER} !^http://(www\.)?pglaf\.org [NC]
RewriteCond %{HTTP_REFERER} !^http://www\.pgdp\.net [NC]
RewriteCond %{HTTP_REFERER} !^http://www\.thegrovelib\.org [NC]
RewriteCond %{HTTP_REFERER} !^http://(onlinebooks|digital)\.library\.upenn\.edu/webbin/gutbook/
RewriteRule ^ - [E=deeplinked:1]

# careful: some of the rewriting rules depend on MultiViews on
# gbn: Options -Indexes +MultiViews +FollowSymLinks -SymLinksIfOwnerMatch
MultiviewsMatch NegotiatedOnly

# gbn: DirectoryIndex index.php

ErrorDocument 404 /error.php
ErrorDocument 403 /error403.html

# remove multiple slashes
RewriteCond %{REQUEST_URI} ^(.*?)//+(.*)$
RewriteRule . %1/%2 [R=301,L]

# No range requests
#RewriteCond %{HTTP:Range} - [OR]
#RewriteCond %{HTTP:Request-Range} -
#RewriteRule ^.* - [F]

Header set Accept-Ranges none

Header set Content-Disposition attachment env=KINDLE

# allow access to error page even if later rules force a 403
RewriteCond %{REQUEST_URI} ^/error403 [NC]
RewriteRule ^.*$ - [L]

<Files "error403*">
  Allow from All
</Files>

<Files "MIRRORS.ALL*">
  Allow from All
</Files>

<Files "robots.txt">
  Allow from All
</Files>

<Files "google3c00b5891470e2e3.html">
  Allow from All
</Files>

# trojan php shell
<Files "4040.php">
  Deny from All
</Files>


RewriteRule ^dirs/(?:[0-9]/)*([0-9]{2,})(?:$|/)(.*)   /files/$1/$2 [R]
RewriteRule      ^(?:[0-9]/)*([0-9]{2,})(?:$|/)(.*)   /files/$1/$2 [R]

# gbn: RewriteRule ^(dirs/.*)   /ratelimiter.php/$1 [NS,L]

# gbn: RewriteCond %{REQUEST_URI} 	!\.(jpg|png|rss)$
# gbn: RewriteRule ^(cache/epub/.*)	/ratelimiter.php/$1 [NS,L]

# RewriteRule ^dirs2/(.*)	 /zipcat3.php/dirs/$1 [L]
# RewriteRule ^files2/(.*) /zipcat3.php/files/$1 [L]

# Server name

# gbn: RewriteCond %{SERVER_NAME} =www.m.exosomatic.info
# gbn: RewriteRule ^(.+) %{SERVER_PROTOCOL}://www.exosomatic.info/%1 [L,R]

# The new search engine
# does engine serve text/html to IE?

RewriteRule ^etext/([0-9]+)$   /ebooks/$1 [L,R=301]

# redirect externally so android sees the right file extension
RewriteRule ^ebooks/([0-9]+)\.epub$           /cache/epub/$1/pg$1.epub [L,R]
RewriteRule ^ebooks/([0-9]+)\.epub\.noimages$ /cache/epub/$1/pg$1.epub [L,R]
RewriteRule ^ebooks/([0-9]+)\.epub\.images$   /cache/epub/$1/pg$1-images.epub [L,R]

RewriteRule ^ebooks/([0-9]+)\.kindle$           /cache/epub/$1/pg$1.mobi [L,R]
RewriteRule ^ebooks/([0-9]+)\.kindle\.noimages$ /cache/epub/$1/pg$1.mobi [L,R]
RewriteRule ^ebooks/([0-9]+)\.kindle\.images$   /cache/epub/$1/pg$1-images.mobi [L,R]

RewriteRule ^ebooks/([0-9]+)\.html\.gen$      /cache/epub/$1/pg$1.html [L,R]
RewriteRule ^ebooks/([0-9]+)\.html\.noimages$ /cache/epub/$1/pg$1.html [L,R]
RewriteRule ^ebooks/([0-9]+)\.html\.images$   /cache/epub/$1/pg$1-images.html [L,R]

RewriteRule ^ebooks/([0-9]+)\.pdf\.gen$       /cache/epub/$1/pg$1.pdf [L,R]
RewriteRule ^ebooks/([0-9]+)\.pdf\.noimages$  /cache/epub/$1/pg$1.pdf [L,R]
RewriteRule ^ebooks/([0-9]+)\.pdf\.images$    /cache/epub/$1/pg$1-images.pdf [L,R]

RewriteRule ^ebooks/([0-9]+)\.rst\.gen$       /cache/epub/$1/pg$1.rst [L,R]

RewriteRule ^ebooks/([0-9]+)\.txt\.utf-8$     /cache/epub/$1/pg$1.txt [L,R]

RewriteRule ^ebooks/([0-9]+)\.rdf$            /cache/epub/$1/pg$1.rdf [L,R]

# use redirect so varnish gets to cache these
RewriteRule ^ebooks/([0-9]+)\.cover.small$    /cache/epub/$1/pg$1.cover.small.jpg [L,R]
RewriteRule ^ebooks/([0-9]+)\.cover.medium$   /cache/epub/$1/pg$1.cover.medium.jpg [L,R]
RewriteRule ^ebooks/([0-9]+)\.qrcode$         /cache/epub/$1/pg$1.qrcode.png [L,R]

# some `private' urls for catalog maintainers
# /etext/12345e => the book edit page
RewriteRule ^e(text|books)/([0-9]+)e$ /catalog/admin/book.php?mode=edit&fk_books=$2 [L,R]
# /etext/12345f => the files edit page
RewriteRule ^e(text|books)/([0-9]+)f$ /catalog/admin/files.php?fk_books=$2 [L,R]


Header unset Content-Location
# for IE, Safari, Chrome, Opera, Mozilla 3.6.9+
Header set X-Frame-Options sameorigin
# for Mozilla
# Header set X-Content-Security-Policy "allow 'self' chart.apis.google.com; frame-ancestors 'self'; options inline-script"

# gbn: php_value include_path ".:/public/vhost/g/gutenberg/private/lib/php:/usr/local/lib/php"
php_value include_path ".:/var/www/gutenberg/lib/php:/usr/local/lib/php"
php_value register_globals 0
php_value magic_quotes_gpc off
php_value error_reporting -1
php_value display_errors 0
php_value log_errors 1
php_value log_errors_max_len 0
php_value error_log "/var/www/gutenberg/log/php_error.log"
php_value html_errors 0
php_value ignore_repeated_errors 1
php_value session.save_path "/var/www/gutenberg/log/sessions"
php_value session.use_trans_sid 0

php_value memory_limit "256M"

# FIXME
php_value mysql.connect_timeout 3
# not allowed php_flag mysql.allow_persistent off
# not allowd php_flag mysqli.reconnect on

AddHandler cgi-script .py
AddHandler default-handler .map

AddType  application/x-httpd-php         .php
AddType  application/x-httpd-php-source  .phps

AddType text/css .css
AddType application/javascript .js
AddType application/rdf+xml .rdf
AddType application/rss+xml .rss
AddType application/marc .marc
AddType text/plain .ALL
AddType text/plain .AUS
AddType text/plain .map
AddType application/epub+zip .epub
AddType application/prs.plucker .pdb .plucker
AddType application/x-qioo-ebook .qioo
AddType application/java-archive .jar
AddType application/x-mobipocket-ebook .mobi
AddType text/html .bibrec
AddType application/xhtml+xml .bibrec-mobile
AddType audio/ogg .ogg .spx .oga
AddType audio/flac .flac
AddType video/ogg .ogv
AddType audio/mp4 .m4b
AddType image/vnd.microsoft.icon .ico
AddType text/x-python .py
AddType text/x-rst .rst
AddType application/xml .xml
AddType application/vnd.android.package-archive .apk

# fonts
AddType font/woff2 .woff2
AddType font/woff .woff
# AddType application/font-woff .woff
AddType application/x-font-ttf .ttf
AddType font/eot .eot

<FilesMatch "\.(woff2|woff|eot|ttf)$">
  Header set X-Content-Type-Options no-sniff
  # See: http://stackoverflow.com/questions/7748140/font-face-eot-not-loading-over-https
  Header unset Vary
</FilesMatch>

# bogus extensions used in old books
AddType image/gif .GIF
AddType image/jpeg .JPG
AddType image/png .PNG

AddDefaultCharset Off
php_value default_charset None

AddCharset UTF-8 .utf8
AddCharset ISO-8859-1 .iso-8859-1

RemoveType .gz

AddEncoding gzip .gz .gzip

FileETag none

ExpiresActive On
# ExpiresDefault "access plus 1 day"

ExpiresByType text/css   "access plus 1 year"
ExpiresByType text/javascript  "access plus 1 year"
ExpiresByType application/javascript  "access plus 1 year"

# ExpiresByType image/jpeg "access plus 7 days"
# ExpiresByType image/png  "access plus 7 days"
# ExpiresByType image/gif  "access plus 7 days"
# ExpiresByType image/vnd.microsoft.icon "access plus 30 days"

# ExpiresByType application/rss+xml     "modification plus 25 hours"
# ExpiresByType application/rdf+xml     "modification plus 25 hours"
# ExpiresByType application/marc        "modification plus 25 hours"

# caveat: don't redirect www-dev.exosomatic.info
# some old stacks always append :80 the the host: field.
## RewriteCond %{HTTP_HOST} !^www(-dev|-test)?\.gutenberg\.org(:80)?$
## RewriteCond %{HTTP_HOST} !^gutenberg-test\.ibiblio\.org(:80)?$
## gbn: this was a problem: RewriteRule ^(.*) http://www.exosomatic.info/$1 [L,R=301]

RewriteRule ^authors?/(.+)$    /ebooks/authors/search/?query=$1 [L,R=301]

# short urls for wiki
RewriteRule ^$ /wiki/Main_Page [L]
RewriteRule ^pt/*$ /wiki/PT_Principal [L]
RewriteRule ^wiki/*$ /w/index.php [L,QSA]
RewriteRule ^wiki/(.*)$ /w/index.php?title=$1 [L,QSA]

# FIXME
#RewriteRule ^$     /ebooks/ [R,L]
#RewriteRule ^pt/*$ /ebooks/ [R,L]
#RewriteRule ^wiki/ /ebooks/ [R,L]
#RewriteRule ^w/    /ebooks/ [R,L]

# workaround for IE8 which requests favicon in every directory
RewriteRule favicon.ico$	/pics/favicon.ico

# gbn: RedirectMatch 301 ^/etext([09][0-9])/?(.*)  http://www.exosomatic.info/dirs/etext$1/$2
# gbn: RedirectMatch 301 ^/([1-9])/?(.*)  http://www.exosomatic.info/dirs/$1/$2
# gbn: RedirectMatch 301 ^/(GUTINDEX.*)   http://www.exosomatic.info/dirs/$1

RewriteRule ^feeds/*$             /wiki/Gutenberg:Feeds [R,L]
RewriteRule ^feeds/(.*)$          /cache/epub/feeds/$1  [R,L]

# RedirectMatch 301 ^/files/?$       http://www.exosomatic.info/wiki/Gutenberg:Files_Repository

Header set Connection "Close" env=nokeepalive
Header set X-Connection "Close" env=nokeepalive

