RewriteEngine On

# Clean URLs for the application
RewriteRule ^$ public/index.php [L]

# Public application views
RewriteRule ^(dashboard|settings|connect|subscribers|comments|automation|utility|campaigns|logs|graph_tester)/?$ public/index.php?view=$1 [L,QSA]

# Standalone UI pages
RewriteRule ^login/?$ login.php [L,QSA]
RewriteRule ^logout/?$ logout.php [L,QSA]
RewriteRule ^account/?$ account.php [L,QSA]
RewriteRule ^admin/?$ admin.php [L,QSA]
RewriteRule ^support/?$ support.php [L,QSA]
RewriteRule ^sync_pages/?$ sync_pages.php [L,QSA]
RewriteRule ^RewriteRule ^terms-of-service/?$ terms-of-service.php [L,QSA]
RewriteRule ^privacy-policy/?$ privacy-policy.php [L,QSA]

# Legacy public/index.php?view=... -> clean view URL
RewriteCond %{THE_REQUEST} \s/+public/index\.php\?view=([^&\s]+) [NC]
RewriteRule ^public/index\.php$ /%1 [R=301,L,NE]

# Legacy UI .php URLs -> clean URLs.
# IMPORTANT: worker.php, subscriber_worker.php, webhook.php and oauth.php are backend/callback endpoints,
# so they intentionally keep their .php extension to avoid breaking Cron and Meta callbacks.
RewriteCond %{THE_REQUEST} \s/+((?:login|logout|account|admin|support|sync_pages|data-deletion|terms-of-service|privacy-policy))\.php(?:[?\s]) [NC]
RewriteRule ^(.+)\.php$ /$1 [R=301,L,NE]
