# Chuyển hướng tất cả request về index.php
# Trừ file tĩnh (css, js, images)

RewriteEngine On

# === TẮT DIRECTORY LISTING TOÀN CỤC ===
Options -Indexes

# === BẢO MẬT: Chặn truy cập trực tiếp vào thư mục nhạy cảm ===
# Chặn truy cập app/ (controllers, models, views)
RewriteRule ^app/ - [F,L]

# Chặn truy cập core/ (framework files, Database.php có DB credentials)
RewriteRule ^core/ - [F,L]

# Chặn truy cập vendor/, config/, tmp/, logs/, sql/, backup/
RewriteRule ^vendor/ - [F,L]
RewriteRule ^config/ - [F,L]
RewriteRule ^tmp/ - [F,L]
RewriteRule ^logs/ - [F,L]
RewriteRule ^sql/ - [F,L]
RewriteRule ^backup/ - [F,L]
RewriteRule ^database/ - [F,L]
RewriteRule ^storage/ - [F,L]

# Chặn truy cập file ẩn (.env, .htaccess backup, .git)
RewriteRule (^\.|/\.) - [F,L]

# Chặn truy cập file nhạy cảm (composer, package, readme, sql dump)
<FilesMatch "\.(sql|log|bak|old|orig|dist|save|swp|lock|json|xml|yml|yaml|ini|conf|sh|md)$">
    Require all denied
</FilesMatch>

# Cho phép assets cần thiết
<FilesMatch "^(manifest\.json|robots\.txt|sitemap\.xml|favicon\.ico)$">
    Require all granted
</FilesMatch>

# === SECURITY HEADERS ===
<IfModule mod_headers.c>
    Header set X-Content-Type-Options "nosniff"
    Header set X-Frame-Options "SAMEORIGIN"
    Header set X-XSS-Protection "1; mode=block"
    Header set Referrer-Policy "strict-origin-when-cross-origin"
</IfModule>

# === ROUTING ===
# Nếu file hoặc thư mục thật tồn tại → phục vụ trực tiếp (CSS, JS, images, uploads)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# Chuyển về index.php với URL parameter
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
