RewriteEngine On

# Redirect www to non-www
RewriteCond %{HTTP_HOST} ^www\.eortv\.com$ [NC]
RewriteRule ^(.*)$ https://eortv.com/$1 [L,R=301]

# CORS Headers
Header set Access-Control-Allow-Origin "*"

# If the requested URL does not have .html or .php and it's not a file or directory, try appending .html first
RewriteCond %{REQUEST_URI} !\.(html|php)$
RewriteCond %{DOCUMENT_ROOT}/$1.html -f
RewriteRule ^([^/]+)$ $1.html [L]

# If .html does not exist, try appending .php
RewriteCond %{REQUEST_URI} !\.(html|php)$
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^([^/]+)$ $1.php [L]

DirectorySlash Off

# Ensure direct requests to PHP or HTML files work
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ /$1.php [L]

RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ /$1.html [L]

# Remove index.html or index.php from URLs
RewriteCond %{THE_REQUEST} \s/+(.*?/)?(?:index)?(.*?)\.(html|php)[\s?/] [NC]
RewriteRule ^(.*)$ /%1%2 [R=302,L]

# Ensure directories are properly handled
RewriteCond %{REQUEST_URI} !(/$|\.) [NC]
RewriteRule ^ %{REQUEST_URI}/ [R=302,L]

# Enable caching
ExpiresActive On