# Enable URL rewriting
RewriteEngine On

# ==============================
# 1. Force HTTPS and www
# ==============================
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\.swarnavahini\.lk$ [NC]
RewriteRule ^(.*)$ https://www.swarnavahini.lk/$1 [L,R=301]

# ==============================
# 2. Serve robots.txt dynamically
# ==============================
RewriteRule ^robots\.txt$ robots.php [L]

# ==============================
# 3. Serve sitemap.xml dynamically
# ==============================
RewriteRule ^sitemap\.xml$ sitemap.php [L]

# ==============================
# 4. Allow Googlebot & Bingbot (fixes HTTP 403)
# ==============================
<IfModule mod_setenvif.c>
    SetEnvIfNoCase User-Agent "Googlebot" allow_bot
    SetEnvIfNoCase User-Agent "Bingbot" allow_bot
</IfModule>

<IfModule mod_authz_core.c>
    <RequireAny>
        Require env allow_bot
        Require all granted
    </RequireAny>
</IfModule>

# ==============================
# 5. Security: prevent access to hidden files (.htaccess, .git, etc.)
# ==============================
<FilesMatch "^\..*">
    Require all denied
</FilesMatch>
