RewriteEngine On

# ---------------------------------------------------------
# 1) Nur Root von *.ismyusbfake.com nach /fakecheck/ umleiten
# ---------------------------------------------------------
RewriteCond %{HTTP_HOST} (^|\.)ismyusbfake\.com$ [NC]
RewriteRule ^$ /fakecheck/ [L,R=301]

# 1b) Subpath weiterreichen: /xyz → /fakecheck/?referrer=xyz
RewriteCond %{HTTP_HOST} (^|\.)ismyusbfake\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/fakecheck(/|$) [NC]
RewriteCond %{REQUEST_URI} ^/([^/]+)/?$ [NC]
RewriteRule ^ /fakecheck/?referrer=%1 [L,R=302]

# ---------------------------------------------------------
# 2) Uploads schützen
# ---------------------------------------------------------
RewriteRule ^uploads/avatar/ - [F]

# ---------------------------------------------------------
# 3) Auth-Routen
# ---------------------------------------------------------
RewriteRule ^auth/login/?$    auth/login.php    [L]
RewriteRule ^auth/register/?$ auth/register.php [L]
RewriteRule ^auth/logout/?$   auth/logout.php   [L]

# ---------------------------------------------------------
# 4) Tools-Regel
#    /tools/foo.php  → landingpage/tools/foo.php
# ---------------------------------------------------------
RewriteCond %{REQUEST_URI} ^/tools/ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^tools/(.*)$ landingpage/tools/$1 [L]

# ---------------------------------------------------------
# 5) Generische Landingpage-Regel
# ---------------------------------------------------------
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/(auth|assets|uploads|landingpage|tools|_errors)/ [NC]
RewriteCond %{REQUEST_URI} ^/([^/]+)/?$ [NC]
RewriteCond %{DOCUMENT_ROOT}/landingpage/%1/index.php -f
RewriteRule ^ landingpage/%1/index.php [L]

# ---------------------------------------------------------
# 6) Fallback-Regeln (.php und /index.php)
# ---------------------------------------------------------
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}/index.php -f
RewriteRule ^(.+?)/?$ $1/index.php [L]

# ---------------------------------------------------------
# 7) Globaler 404-Fallback **IMMER deine eigene Fehlerseite**
# ---------------------------------------------------------
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/(assets|uploads|auth|landingpage|tools|_errors)/ [NC]
RewriteRule ^ _errors/404.php [L]
