# Set index.n to come before index.php

DirectoryIndex index.n index.php index.html

RewriteEngine On

# Redirect http to https
RewriteBase /
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTP_HOST} !^localhost(:[0-9]+)?$
RewriteCond %{REQUEST_URI} !index\.n/?$
RewriteCond %{REQUEST_URI} !^/api/
RewriteCond %{REQUEST_URI} !^/files/
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=302,L,QSA]

# /files are stored in S3

# for recent versions of haxelib client, which send UA, use https, and support redirection
RewriteBase /
RewriteCond %{HTTPS} =on [OR]
RewriteCond %{HTTP:X-Forwarded-Proto} =https
RewriteCond %{ENV:HAXELIB_CDN} !^$
RewriteCond %{HTTP_USER_AGENT} !^$
RewriteRule "^files/(.*)$" "https://%{ENV:HAXELIB_CDN}/files/$1" [L,R=302]

# for older versions of haxelib client
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{ENV:HAXELIB_CDN} !^$
RewriteRule "^files/(.*)$" "http://%{ENV:HAXELIB_CDN}/files/$1" [P]

# Rewrite rules to send Haxe remoting calls on "/" to "haxelib1/".  If it matches, that's [L], the last rule - go their immediately.

RewriteBase /
RewriteCond %{HTTP:X-Haxe-Remoting}: 1
RewriteRule ^index\.n$ legacy/index.n [L]

# Enable rewrite for the new site's pages, only if the file or dir doesn't exist

RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.n/$1 [L]

# Get our stuff gzipped

SetOutputFilter DEFLATE
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript application/javascript