# Enable URL rewriting
RewriteEngine On

# ==============================
# IMPORTANT: REWRITE BASE
# ==============================
RewriteBase /

# ==============================
# 0. FORCE REMOVAL of "noindex"
# ==============================
<IfModule mod_headers.c>
    Header unset X-Robots-Tag
</IfModule>

# ==============================
# 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. REMOVE "index.php" FROM URL (NEW ADDITION)
# ==============================
# Triggers only if the user specifically requested /index.php
RewriteCond %{THE_REQUEST} ^.*/index\.php 
RewriteRule ^(.*)index.php$ /$1 [R=301,L]

# ==============================
# 3. TELEDRAMA SLUG REWRITING
# ==============================
# Ignore real files/folders
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# This looks for "teledrama_detail.php" in the SAME folder as this .htaccess file
RewriteRule ^teledramas/([^/]+)/?$ teledrama_detail.php?slug=$1 [L,QSA]

# ==============================
# 4. PROGRAMMES SLUG REWRITING
# ==============================
# Redirect "programmes/slug" to programme_detail.php
RewriteRule ^programmes/([^/]+)/?$ programme_detail.php?slug=$1 [L,QSA]

# ==============================
# 5. Serve robots.txt & sitemap.xml
# ==============================
RewriteRule ^robots\.txt$ robots.php [L]
RewriteRule ^sitemap\.xml$ sitemap.php [L]

# ==============================
# 6. EXPLICITLY ALLOW ACCESS TO PHP
# ==============================
<FilesMatch "\.php$">
    <IfModule mod_authz_core.c>
        Require all granted
    </IfModule>
</FilesMatch>

# ==============================
# 7. Security: Block hidden files
# ==============================
<FilesMatch "^\..*">
    <IfModule mod_authz_core.c>
        Require all denied
    </IfModule>
</FilesMatch>

# php -- BEGIN cPanel-generated handler
<IfModule mime_module>
  AddHandler application/x-httpd-ea-php74 .php .php7 .phtml
</IfModule>
# php -- END cPanel-generated handler

# php -- BEGIN cPanel-generated handler, do not edit
# Set the “ea-php74” package as the default “PHP” programming language.
<IfModule mime_module>
  AddHandler application/x-httpd-ea-php74 .php .php7 .phtml
</IfModule>
# php -- END cPanel-generated handler, do not edit
