<IfModule mod_rewrite.c>
    RewriteEngine On

    # 1. Fix SSL / HTTPS Redirect (Supports Cloudflare, Flexible SSL & Proxy Headers)
    RewriteCond %{HTTP:X-Forwarded-Proto} !https
    RewriteCond %{HTTPS} off
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

    # 2. Force WWW ONLY for main root domain (NOT for subdomains like billing.unicareagencies.com)
    RewriteCond %{HTTP_HOST} ^unicareagencies\.com$ [NC]
    RewriteRule ^ https://www.unicareagencies.com%{REQUEST_URI} [L,R=301]

    # 3. Redirect www.billing back to billing (Clean subdomain)
    RewriteCond %{HTTP_HOST} ^www\.billing\.unicareagencies\.com$ [NC]
    RewriteRule ^ https://billing.unicareagencies.com%{REQUEST_URI} [L,R=301]

    # 4. Redirect /public/ URI if accessed directly to clean URL
    RewriteCond %{THE_REQUEST} /public/([^\s?]*) [NC]
    RewriteRule ^ %1 [L,R=301]

    # 5. Safely forward requests to public/ directory
    RewriteCond %{REQUEST_URI} !^/public/
    RewriteRule ^(.*)$ public/$1 [L]
</IfModule>

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