RewriteEngine On

# -----------------------------------------------------------------------------
# Force HTTPS (universal: Apache, cPanel, Cloudflare, reverse proxy)
# -----------------------------------------------------------------------------
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

# -----------------------------------------------------------------------------
# Redirect all requests with a trailing slash to the same URL without the slash
# -----------------------------------------------------------------------------
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/$
RewriteCond %{REQUEST_URI} ^(.+)/$
RewriteRule ^(.*)/$ /$1 [R=301,L]

# -----------------------------------------------------------------------------
# Hide .html in URLs and serve files without extensions
# -----------------------------------------------------------------------------
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^([^/]+)/?$ $1.html [L]

# -----------------------------------------------------------------------------
# Redirect requests with .html to URLs without extensions
# -----------------------------------------------------------------------------
RewriteCond %{THE_REQUEST} \s/([^.]+)\.html [NC]
RewriteRule ^ /%1 [R=301,L]

# -----------------------------------------------------------------------------
# Serve .html files if they exist
# -----------------------------------------------------------------------------
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^([^\.]+)$ $1.html [NC,L]

# -----------------------------------------------------------------------------
# Serve .php files if they exist
# -----------------------------------------------------------------------------
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

# -----------------------------------------------------------------------------
# Redirect root to home.html
# -----------------------------------------------------------------------------
RewriteCond %{REQUEST_URI} ^/$
RewriteCond %{DOCUMENT_ROOT}/home.html -f
RewriteRule ^$ /home.html [L]

# -----------------------------------------------------------------------------
# Disable directory listing
# -----------------------------------------------------------------------------
Options -Indexes

# -----------------------------------------------------------------------------
# PHP handler (cPanel)
# -----------------------------------------------------------------------------
<IfModule mime_module>
  AddHandler application/x-httpd-ea-php74 .php .php7 .phtml
</IfModule>