# Enable Apache's Rewrite Engine
RewriteEngine On

# Define the base URL for rewrite rules in this .htaccess.
# This must be the path from your server's DocumentRoot to THIS .htaccess file.
# For XAMPP with htdocs/CoreFlux/, it should be:
RewriteBase /ercs/

# 1. Block direct access to sensitive files (e.g., .env, composer files, .htaccess itself)
# These files contain configuration and dependencies and should NEVER be web-accessible.
<Files ".env">
    Order allow,deny
    Deny from all
</Files>
<Files "composer.json">
    Order allow,deny
    Deny from all
</Files>
<Files "composer.lock">
    Order allow,deny
    Deny from all
</Files>
<Files ".htaccess"> # Good practice to block access to .htaccess files themselves
    Order allow,deny
    Deny from all
</Files>
# Allow robots.txt and sitemap.xml but block other sensitive file types
<FilesMatch "^(?!robots\.txt$|sitemap\.xml$).*\.(ini|log|txt|md)$">
    Order allow,deny
    Deny from all
</FilesMatch>



# 2. Block direct access to sensitive directories (return 403 Forbidden)
# These rules ensure nobody can browse into your application, core, vendor, storage, or test folders.
# The `.*` after the folder name ensures all contents within are also blocked.
# `[F,L]` means Forbidden (403 status code) and Last rule (stop processing).
# Note: 'config', 'routes', 'services', 'views', 'models', 'controllers' are subfolders of 'app/' in your structure.
# Blocking 'app/.*$' covers them. Only explicitly block them here if they exist as top-level directories.
RewriteRule ^app/.*$ - [F,L]
RewriteRule ^core/.*$ - [F,L]
RewriteRule ^vendor/.*$ - [F,L]
RewriteRule ^storage/.*$ - [F,L]
RewriteRule ^tests/.*$ - [F,L]


# 3. Direct all other requests to the public/ directory
# This is the main routing rule for your application.
# It only applies if the requested path is NOT an actual existing file or directory in the root.
# CRUCIAL: This condition prevents an infinite loop by NOT rewriting if the URI already starts with the public path.
RewriteCond %{REQUEST_URI} !^/CoreFlux/public/ [NC] # <-- This is the most reliable loop prevention
# RewriteCond %{REQUEST_FILENAME} !-d # If the request is NOT for an existing directory
# RewriteCond %{REQUEST_FILENAME} !-f # And the request is NOT for an existing file
# Then, internally rewrite the request to point inside the 'public/' folder.
# ^(.*)$ captures the entire requested path. public/$1 prepends 'public/' to it.
# [L] stops processing further rules.
RewriteRule ^(.*)$ public/$1 [L]