RewriteEngine On

# --- 1. Force HTTPS ---
# If a user comes via HTTP, send them to HTTPS immediately
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# --- 2. Force non-www ---
# If user types www.kinlyr.com, send them to kinlyr.com
# This merges "two" sites into one, fixing the Duplicate error.
RewriteCond %{HTTP_HOST} ^www\.kinlyr\.com [NC]
RewriteRule ^(.*)$ https://kinlyr.com/$1 [L,R=301]

# --- 3. Remove "index.html" ---
# If the URL ends in /index.html, strip it to avoid duplicates
RewriteCond %{THE_REQUEST} ^.*/index\.html
RewriteRule ^(.*)index.html$ /$1 [R=301,L]

# --- 4. External Redirect (Strip .html extension) ---
# If browser requests /about.html, redirect the user to /about
# We use THE_REQUEST to ensure we only redirect external requests (preventing loops)
RewriteCond %{THE_REQUEST} \s/+(.+?)\.html[\s?] [NC]
RewriteRule ^ /%1 [R=301,L]

# --- 5. Internal Rewrite (Map clean URL to file) ---
# When user requests /about, silently serve about.html from the server
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html [NC,L]