<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /

    # Redirect www to non-www AND HTTP to HTTPS
    RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
    RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L]

    RewriteCond %{HTTPS} off
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

    # Remove trailing slashes (if not a directory)
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} ^(.+)/$ [NC]
    RewriteRule ^ %1 [R=301,L]

    # Redirect .php requests to clean URLs
    RewriteCond %{THE_REQUEST} /([^.]+)\.php [NC]
    RewriteRule ^ /%1 [R=301,L]

    # Serve .php files if they exist
    RewriteCond %{REQUEST_FILENAME}.php -f
    RewriteRule ^([^/]+)/?$ $1.php [L]

    # Redirect .html requests to clean URLs
    RewriteCond %{THE_REQUEST} /([^.]+)\.html [NC]
    RewriteRule ^ /%1 [R=301,L]

    # Serve .html files if they exist
    RewriteCond %{REQUEST_FILENAME}.html -f
    RewriteRule ^([^/]+)/?$ $1.html [L]

    # Redirect all non-existing URLs to /404.php (with redirect)
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^.*$ /404.php [R=302,L]
</IfModule>

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