# dpdownloader — server configuration

Options -Indexes
DirectoryIndex index.php index.html

RewriteEngine On

# Canonical host, https only
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

# Trailing slash on directory URLs, so /youtube-... and /youtube-.../ do not
# both get indexed as separate pages
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^(.*)$ /$1/ [R=301,L]

RewriteRule ^sitemap\.xml$ /sitemap.php [L]

# Download links. The visitor sees /d/{token}/{filename}; nothing about
# the origin CDN appears anywhere in the url.
RewriteRule ^d/([A-Za-z0-9_-]+)/[^/]*$ /dp-image.php?k=$1 [QSA,L]

# The proxy is only ever reached through the rule above.
RewriteRule ^dp-image\.php$ - [F]

# Compression
<IfModule mod_deflate.c>
  AddOutputFilterByType DEFLATE text/html text/css text/plain text/xml
  AddOutputFilterByType DEFLATE application/javascript application/json
  AddOutputFilterByType DEFLATE image/svg+xml
</IfModule>

# Caching. Assets are versioned with ?v= so they can be cached hard.
<IfModule mod_expires.c>
  ExpiresActive On
  ExpiresByType text/css            "access plus 1 year"
  ExpiresByType application/javascript "access plus 1 year"
  ExpiresByType image/png           "access plus 6 months"
  ExpiresByType image/svg+xml       "access plus 6 months"
  ExpiresByType image/x-icon        "access plus 6 months"
  ExpiresByType text/html           "access plus 0 seconds"
</IfModule>

<IfModule mod_headers.c>
  Header set X-Content-Type-Options "nosniff"
  Header set Referrer-Policy "strict-origin-when-cross-origin"
  Header set X-Frame-Options "SAMEORIGIN"
  Header always unset X-Powered-By
</IfModule>

# Downloads must never be indexed.
<FilesMatch "^(dp-image|dp-api|pin-api)\.php$">
  Header set X-Robots-Tag "noindex, nofollow"
</FilesMatch>

ErrorDocument 404 /404.php
