# Enable URL rewriting
RewriteEngine On

# ==============================
# IMPORTANT: REWRITE BASE
# ==============================
RewriteBase /
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /

    # Redirect /childrensday to the actual php file inside pages/
    RewriteRule ^childrensday/?$ pages/competition_register.php [L,NC]
    # Rewrite clean URL for competition entries admin page
# Rewrite /competition-entries to the admin script
RewriteRule ^competition-entries/?$ admin/competition_entries.php [L,NC]
</IfModule>
# ==============================
# 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

# BEGIN cPanel-generated php ini directives, do not edit
# Manual editing of this file may result in unexpected behavior.
# To make changes to this file, use the cPanel MultiPHP INI Editor (Home >> Software >> MultiPHP INI Editor)
# For more information, read our documentation (https://go.cpanel.net/EA4ModifyINI)
<IfModule php7_module>
   php_flag display_errors Off
   php_value max_execution_time 300
   php_value max_input_time 60
   php_value max_input_vars 1000
   php_value memory_limit 32M
   php_value post_max_size 500M
   php_value session.gc_maxlifetime 1440
   php_value session.save_path "/var/cpanel/php/sessions/ea-php74"
   php_value upload_max_filesize 500M
   php_flag zlib.output_compression Off
</IfModule>
<IfModule lsapi_module>
   php_flag display_errors Off
   php_value max_execution_time 300
   php_value max_input_time 60
   php_value max_input_vars 1000
   php_value memory_limit 32M
   php_value post_max_size 500M
   php_value session.gc_maxlifetime 1440
   php_value session.save_path "/var/cpanel/php/sessions/ea-php74"
   php_value upload_max_filesize 500M
   php_flag zlib.output_compression Off
</IfModule>
# END cPanel-generated php ini directives, do not edit

# 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
