# SeaHen: Apache configuration for the public web root.
Options -Indexes -MultiViews
DirectoryIndex index.php

<IfModule mod_rewrite.c>
RewriteEngine On

# This folder's URL path (/ at a domain root, /v2/seahen/public/ in a subfolder), used by the redirects below.
RewriteCond %{REQUEST_URI}::$1 ^(.*?/)(.*)::\2$
RewriteRule ^(.*)$ - [E=SH_BASE:%1]

# Always use HTTPS and the bare domain.
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP:X-Forwarded-Proto} !=https
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,R=301]

# Old-style addresses redirect to clean ones: index.php -> home, about.php -> about
# (only for the address the visitor typed, not for the site's own internal rewrites)
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{THE_REQUEST} ^GET\s[^?\s]*/index\.php[\s?]
RewriteRule ^index\.php$ %{ENV:SH_BASE} [L,R=301]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{THE_REQUEST} ^GET\s[^?\s]*/([a-z0-9-]+)\.php[\s?]
RewriteRule ^([a-z0-9-]+)\.php$ %{ENV:SH_BASE}%1 [L,R=301]

# Trailing slash -> none
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ %{ENV:SH_BASE}$1 [L,R=301]

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

# Clean URLs map to their PHP files.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([a-z0-9-]+)$ $1.php [L]

# Anything else that doesn't exist gets the site's own 404 page.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ 404.php [L]
</IfModule>

# Never serve dotfiles (.env, .git, .htaccess) or internal files starting with an underscore.
<FilesMatch "^[._]">
    Require all denied
</FilesMatch>

<IfModule mod_headers.c>
    Header always set X-Content-Type-Options "nosniff"
    Header always set Referrer-Policy "strict-origin-when-cross-origin"
    Header always unset X-Powered-By
    <FilesMatch "\.(css|js|woff2|svg|avif|webp|jpg|png)$">
        Header set Cache-Control "public, max-age=31536000, immutable"
    </FilesMatch>
</IfModule>

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

AddType font/woff2 .woff2
AddType image/avif .avif
AddType image/webp .webp
