# BroadcastEvent - Apache/LiteSpeed Configuration
# SEO-friendly URL routing with fallback support
# Compatible with Apache 2.2+, LiteSpeed, OpenLiteSpeed

# Prefer index.php as the front controller.
# Keep .html/.htm PHP handling only as a legacy fallback on servers that allow it.

# Allow PHP files in this directory
<FilesMatch "\.(php)$">
    Require all granted
</FilesMatch>

# Enable Rewrite Engine
<IfModule mod_rewrite.c>
    RewriteEngine On

    # Disable MultiViews content-negotiation: on cPanel it can intercept
    # requests before mod_rewrite and break front-controller routing.
    <IfModule mod_negotiation.c>
        Options -MultiViews
    </IfModule>

    # Dynamic RewriteBase - auto-detect from current directory
    RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
    RewriteRule ^(.*) - [E=BASE:%1]

    # Force HTTPS (uncomment in production)
    # RewriteCond %{HTTPS} off
    # RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

    # Remove trailing slash (except for directories)
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.+)/$ %{ENV:BASE}/$1 [L,R=301]

    # Update routes: /update/sitemap
    RewriteRule ^update/sitemap/?$ index.php?update=sitemap [QSA,L]

    # Folder-prefixed slug route: /index.php/ufabet
    # Some deployments use "index.php" as the public folder name. Route its
    # first child segment through the root front controller just like /ufabet.
    RewriteRule ^index\.php/([^/]+)/?$ index.php?view=$1 [QSA,L]

    # Handle existing files and directories directly
    RewriteCond %{REQUEST_FILENAME} -f [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^ - [L]

    # Skip rewriting for known static files and PHP scripts, so real files in
    # this directory are served/executed directly instead of treated as slugs.
    RewriteRule \.(php|xml|txt|ico|png|jpg|jpeg|gif|webp|svg|css|js|woff|woff2|html|htm|pdf)$ - [L]

    # Direct slug route: /slug (single segment, supports Unicode/Thai characters)
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^([^/]+)/?$ index.php?view=$1 [QSA,L]
</IfModule>

# Deny access to sensitive files
<FilesMatch "\.(htaccess|htpasswd|ini|log|sh|sql|bak|config|env|json|txt)$">
    <IfModule mod_authz_core.c>
        Require all denied
    </IfModule>
    <IfModule !mod_authz_core.c>
        Order Allow,Deny
        Deny from all
    </IfModule>
</FilesMatch>

# Allow access to specific files
<FilesMatch "^(sitemap\.xml|robots\.txt|favicon\.(ico|png)|[a-f0-9]{32}\.txt)$">
    <IfModule mod_authz_core.c>
        Require all granted
    </IfModule>
    <IfModule !mod_authz_core.c>
        Order Allow,Deny
        Allow from all
    </IfModule>
</FilesMatch>

# GZIP Compression
<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/html text/plain text/css
    AddOutputFilterByType DEFLATE text/javascript application/javascript application/json
    AddOutputFilterByType DEFLATE application/xml text/xml image/svg+xml
</IfModule>

# Cache Control
<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresByType text/html "access plus 0 seconds"
    ExpiresByType text/css "access plus 1 month"
    ExpiresByType text/javascript "access plus 1 month"
    ExpiresByType application/javascript "access plus 1 month"
    ExpiresByType image/png "access plus 1 year"
    ExpiresByType image/jpeg "access plus 1 year"
    ExpiresByType image/gif "access plus 1 year"
    ExpiresByType image/svg+xml "access plus 1 year"
    ExpiresByType image/webp "access plus 1 year"
    ExpiresByType image/x-icon "access plus 1 year"
    ExpiresByType font/woff2 "access plus 1 year"
    ExpiresByType font/woff "access plus 1 year"
</IfModule>

# Prevent directory listing
Options -Indexes

# Set default charset
AddDefaultCharset UTF-8
