RewriteEngine On
RewriteBase /

# Retired URLs jump straight to their final canonical destination on every host.
RewriteRule ^portfolio/?$ https://evanjohnston.media/work [R=301,L,NE]
RewriteRule ^work/volvo-stories/?$ https://evanjohnston.media/work/corporate-communications-at-volvo [R=301,L,NE]

# Remove a trailing slash and correct host/protocol in the same hop.
RewriteCond %{REQUEST_URI} !^/$
RewriteCond %{REQUEST_URI} !^/(?:admin|portal|portal-api|demos|assets|images|PHPMailer-7\.0\.2)(?:/|$) [NC]
RewriteRule ^(.+)/$ https://evanjohnston.media/$1 [R=301,L,NE]

# One preferred protocol and host for already slashless URLs.
RewriteCond %{HTTPS} !=on [OR]
RewriteCond %{HTTP_HOST} !^evanjohnston\.media$ [NC]
RewriteRule ^ https://evanjohnston.media%{REQUEST_URI} [R=301,L,NE]

DirectorySlash Off

# Serve generated route HTML without exposing /index.html or adding a slash.
RewriteCond %{REQUEST_FILENAME}/index.html -f
RewriteRule ^(.+?)/?$ $1/index.html [L]

# Authenticated applications keep client-side routing and stay out of search.
RewriteRule ^(?:admin(?:/.*)?|portal)$ index.html [L]

<IfModule mod_headers.c>
  SetEnvIf Request_URI "^/(?:admin|portal|portal-api|demos)(?:/|$)" PRIVATE_ROUTE
  Header set X-Robots-Tag "noindex, nofollow, noarchive" env=PRIVATE_ROUTE
</IfModule>

ErrorDocument 404 /404.html
