AddType text/plain .phps
AddType application/x-tgz .tgz
AddType application/x-chrome-extension .crx
AddType application/x-web-app-manifest+json .webapp

<filesMatch "^(index|preactalpha)\.html$">
  FileETag None
  <ifModule mod_headers.c>
     Header unset ETag
     Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate"
     Header set Pragma "no-cache"
     Header set Expires "Wed, 11 Jan 1984 05:00:00 GMT"
  </ifModule>
</filesMatch>

<filesMatch "colors\.json$">
    Header set Access-Control-Allow-Origin "*" 
    Header set Access-Control-Allow-Methods "GET,POST,OPTIONS"
</filesMatch>

<IfModule mod_rewrite.c>
RewriteEngine on

RewriteCond %{HTTP:X-Forwarded-Proto} ^http$
RewriteCond %{HTTP_HOST} ^play\.pokemonshowdown\.com$ [NC]
RewriteRule ^([A-Za-z0-9-]*)$ https://play.pokemonshowdown.com/$1 [R=307,NE,L]

RewriteCond %{HTTP:X-Forwarded-Proto} ^https$
RewriteCond %{HTTP_HOST} ^play\.pokemonshowdown\.com$ [NC]
RewriteCond %{QUERY_STRING} ^insecure [NC]
RewriteRule ^([A-Za-z0-9-]*)$ http://insecure.psim.us/$1 [R=307,NE,L]

# redirects (keep in sync with client.js)
RewriteRule ^appeals?\/?$ https://play.pokemonshowdown.com/view-help-request--appeal [R=302,L]
RewriteRule ^report\/?$ https://play.pokemonshowdown.com/view-help-request--report [R=302,L]
RewriteRule ^requesthelp\/?$ https://play.pokemonshowdown.com/view-help-request--other [R=302,L]
RewriteRule ^rooms?suggestions?\/?$ https://www.smogon.com/forums/threads/room-suggestions-are-no-longer-being-taken.3522130/ [R=302,L]
RewriteRule ^suggestions?\/?$ https://www.smogon.com/forums/forums/517/ [R=302,L]
RewriteRule ^adminrequests?\/?$ https://www.smogon.com/forums/threads/names-passwords-rooms-and-servers-contacting-upper-staff.3538721/ [R=302,L]
RewriteRule ^forgotpassword\/?$ https://www.smogon.com/forums/threads/names-passwords-rooms-and-servers-contacting-upper-staff.3538721/post-6227626/ [R=302,L]
RewriteRule ^bugs?(reports?)?\/?$ https://www.smogon.com/forums/forums/876/ [R=302,L]
RewriteRule ^formatsuggestions/?$ https://pokemonshowdown.com/pages/formatsuggestions [R=302,L]
RewriteRule ^rules?\/?$ https://pokemonshowdown.com/rules [R=302,L]
RewriteRule ^faq\/?$ https://www.smogon.com/forums/posts/6774128/ [R=302,L]
RewriteRule ^credits?\/?$ https://pokemonshowdown.com/credits [R=302,L]
RewriteRule ^news\/?$ https://pokemonshowdown.com/news [R=302,L]
RewriteRule ^privacy\/?$ https://pokemonshowdown.com/privacy [R=302,L]
RewriteRule ^contact\/?$ https://pokemonshowdown.com/contact [R=302,L]
RewriteRule ^dex\/?$ https://dex.pokemonshowdown.com/ [R=302,L]
RewriteRule ^(damage)?calc\/?$ https://calc.pokemonshowdown.com/ [R=302,L]
RewriteRule ^insecure\/?$ http://play.pokemonshowdown.com/?insecure [R=302,L]
RewriteRule ^devdiscord\/?$ https://discord.com/invite/D8QwhsH [R=302,L]
RewriteRule ^smogcord\/?$ https://discord.gg/smogon [R=302,L]
RewriteRule ^smogdex\/?$ https://smogon.com/dex/ [R=302,L]
RewriteRule ^forums?\/?$ https://smogon.com/forums/ [R=302,L]
RewriteRule ^replays?\/?$ https://replay.pokemonshowdown.com/ [R=302,L]
RewriteRule ^trustworthy\-dlc\-link\/?$ https://www.youtube.com/watch?v=dQw4w9WgXcQ [R=302,L]

# teams shortlinks
RewriteRule ^t\/?$ https://teams.pokemonshowdown.com/ [R=302,L]
RewriteRule ^t\/([a-z0-9-]*)$ https://teams.pokemonshowdown.com/view/$1 [R=302,L]
# replays shortlinks
RewriteRule ^r\/?$ https://replay.pokemonshowdown.com/ [R=302,L]
RewriteRule ^r\/([a-z0-9-]*)$ https://replay.pokemonshowdown.com/$1 [R=302,L]

# rewrite old sprite directories/files
RewriteRule ^sprites\/xyani(.*)?$ sprites/ani$1 [L,QSA]
RewriteRule ^sprites\/xydex(.*)?$ sprites/dex$1 [L,QSA]
RewriteRule ^sprites\/smicons(.*)?$ sprites/pokemonicons$1 [L,QSA]
RewriteRule ^sprites\/trainers\/latest\/?$ /sprites/trainers/?filter=recent [R=302,L,QSA]
RewriteRule ^sprites\/trainers\/credits\/?$ /sprites/trainers/?filter=credited [R=302,L,QSA]

RewriteRule ^sprites\/rby(.*)?$ sprites/gen1$1 [L,QSA]
RewriteRule ^sprites\/gsc(.*)?$ sprites/gen2$1 [L,QSA]
RewriteRule ^sprites\/rse(.*)?$ sprites/gen3$1 [L,QSA]
RewriteRule ^sprites\/dpp(.*)?$ sprites/gen4$1 [L,QSA]
RewriteRule ^sprites\/bw(.*)?$ sprites/gen5$1 [L,QSA]
RewriteRule ^sprites\/xy(.*)?$ sprites/gen6$1 [L,QSA]
RewriteRule ^sprites\/sm(.*)?$ sprites/gen7$1 [L,QSA]

# fill in missing backsprites
RewriteRule ^sprites\/gen1rg-back(.*)?$ sprites/gen1-back$1 [L,QSA]
RewriteRule ^sprites\/gen1rb-back(.*)?$ sprites/gen1-back$1 [L,QSA]
RewriteRule ^sprites\/gen2g-back(.*)?$ sprites/gen2-back$1 [L,QSA]
RewriteRule ^sprites\/gen2s-back(.*)?$ sprites/gen2-back$1 [L,QSA]
RewriteRule ^sprites\/gen3rs-back(.*)?$ sprites/gen3-back$1 [L,QSA]
RewriteRule ^sprites\/gen3frlg-back(.*)?$ sprites/gen3-back$1 [L,QSA]
RewriteRule ^sprites\/gen4dp-back(.*)?$ sprites/gen4-back$1 [L,QSA]
RewriteRule ^sprites\/gen4dp-2-back(.*)?$ sprites/gen4-back$1 [L,QSA]

# FRLG only added new sprites for Kanto Pokemon
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^sprites\/gen3frlg(.*)?$ sprites/gen3$1 [L,QSA]

RewriteCond %{HTTP_HOST} ^www\.play\.pokemonshowdown\.com$ [NC]
RewriteRule ^(.*) https://play.pokemonshowdown.com/$1 [R=301,L]

RewriteRule ^style/fonts?/.*?\.(eot|svg|ttf|woff|woff2)$ - [E=SAFE_RESOURCE:1]
RewriteRule ^data\/.*\.js(?:on)?$ - [E=SAFE_RESOURCE:1]
Header set Access-Control-Allow-Origin * env=SAFE_RESOURCE
Header set Access-Control-Allow-Methods "GET,POST,OPTIONS"
Header set Access-Control-Allow-Headers "Content-Type, X-Requested-With"

# Redirect old battles to their corresponding replay
# RewriteCond %{HTTP_HOST} ^play\.pokemonshowdown\.com$ [NC]
# RewriteRule ^battle-([a-z0-9]+-[1-6][0-9]{8})$ https://replay.pokemonshowdown.com/$1 [R=302,L]
RewriteCond %{HTTP_HOST} ^play\.pokemonshowdown\.com$ [NC]
RewriteRule ^battle-([a-z0-9]+-[0-9]{8})$ https://replay.pokemonshowdown.com/$1 [R=302,L]
RewriteCond %{HTTP_HOST} ^play\.pokemonshowdown\.com$ [NC]
RewriteRule ^battle-([a-z0-9]+)$ https://replay.pokemonshowdown.com/$1 [R=302,L]

# TODO: fix bug where you can't join lobby directly
RewriteRule ^lobby/?$ / [R=302,L]

# roomid with a slash after it: remove the slash, unless the roomid
# is also an actual directory
RewriteCond %{DOCUMENT_ROOT}/$1 !-d
RewriteRule ^([A-Za-z0-9][A-Za-z0-9-]*)/$ /$1 [R=301,L]

# Anything that looks like a roomid: connect to the server
RewriteCond %{HTTP_COOKIE} ^.*preactalpha=1.*$
RewriteRule ^(|[A-Za-z0-9][A-Za-z0-9-]*)$ ./preactalpha.html [L,E=INDEX_PAGE:1]
RewriteRule ^$ - [E=INDEX_PAGE:1]
RewriteRule ^(preactalpha|preactbeta|beta|dev|development|login|users|(dm|challenge|user|viewuser|ladder)-[a-z0-9-]*)$ ./preactalpha.html [L,E=INDEX_PAGE:1]
RewriteRule ^([A-Za-z0-9][A-Za-z0-9-]*)$ ./ [L,E=INDEX_PAGE:1]

# RewriteRule ^crossprotocol\.html$ - [E=CROSSPROTOCOL:1]
# Header set X-Frame-Options "ALLOW-FROM http://play.pokemonshowdown.com" env=CROSSPROTOCOL

# Headers for the index page.
Header set Cache-Control "no-cache, no-store, must-revalidate" env=INDEX_PAGE
Header set Pragma "no-cache" env=INDEX_PAGE
Header set Expires "0" env=INDEX_PAGE

# No direct linking to the lobby.
# RewriteCond %{ENV:SCRIPT_URL} ^/(lobby/?)?$
# RewriteCond %{HTTP_REFERER} !^$
# RewriteCond %{HTTP_REFERER} !^https?:\/\/([a-z0-9-]+.)?(pokemonshowdown\.com|# appjs)
# RewriteCond %{HTTP_HOST} ^play\.pokemonshowdown\.com$
# RewriteCond %{REMOTE_ADDR} !=127.0.0.1
# RewriteCond %{REMOTE_ADDR} !=162.243.13.96
# RewriteCond %{HTTP:CF-Connecting-IP} !=173.252.196.254
# RewriteCond %{HTTP:CF-Connecting-IP} !=198.27.67.31
# RewriteCond %{HTTP:CF-Connecting-IP} !=162.243.13.96
# RewriteRule ^.* https://pokemonshowdown.com/ [R=303,L]

AddType 'text/plain; charset=UTF-8' json5

RewriteRule ^replay/battle-([A-Za-z0-9-]+)$ https://replay.pokemonshowdown.com/$1 [R=302,L]
RewriteRule ^replay/turn_(.+)\.png$ replay/turn-image.php?data=$1 [L,QSA]

RewriteCond %{QUERY_STRING} !output=html
RewriteRule ^ladder\.php$ https://pokemonshowdown.com/ladder/ [R=301,L]

RewriteRule ^~~([^:/]*)(:[0-9]*)?/action\.php$ action.php?serverid=$1 [L,QSA]
RewriteRule ^~~([^/]*)(?:/(.*))?$ redirect-server.php?host=$1&roomid=$2 [L,QSA]

RewriteRule ^backup/ - [F]
RewriteRule ^\.git/ - [F]
RewriteRule ^lib/ - [F]

RewriteRule ^index\.template\.html$ - [F]
RewriteRule ^preactalpha\.template\.html$ - [F]
RewriteRule ^testclient\.html$ - [F]
RewriteRule ^testclient-beta\.html$ - [F]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(apple-touch-icon-precomposed\.png)$ - [R=404,L]

</IfModule>

DirectoryIndex index.php index.html /dirindex/dirindex.php
ErrorDocument 404 /dirindex/404.html
