# Enable Rewrite Engine
RewriteEngine On
RewriteBase /

# Add trailing slash to directories
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ $1/ [L,R=301]

# Remove .html extension if present in URL
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.html [NC]
RewriteRule ^ %1 [R=301,L]

# Internally rewrite URLs without .html to .html files
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*?)/?$ $1.html [L]

# Default index file
DirectoryIndex index.html

# Prevent directory browsing
Options -Indexes

# Custom error pages (optional)
ErrorDocument 404 /404.html
