# ============================================================
# PharmaWholesale MVC - Apache configuration
#
# Two URL styles are both supported, forever, via the single front
# controller (index.php):
#
#   1. Legacy "name.php" URLs (e.g. suppliers.php, invoice_view.php?id=5)
#      -> index.php?route=name&...   (every old bookmark/link/README
#         instruction keeps working unchanged)
#
#   2. Pretty REST-ish paths (e.g. /supplier, /supplier/5/edit,
#      /purchase-order/create) -> index.php?_path=... (the app's
#      internal Router resolves these against app/routeMap.php and
#      dispatches the same Controller/action as the legacy URL would)
#
# Also blocks direct access to the app/ and logs/ source directories.
# ============================================================

Options -Indexes
DirectoryIndex index.php

<IfModule mod_rewrite.c>
 RewriteEngine On
 # RewriteBase intentionally omitted so Apache auto-detects the correct
 # prefix whether this app lives at the domain root or in a subfolder
 # (e.g. /projects/7/). Setting it to "/" breaks subfolder installs.

 # Never rewrite requests for real files/directories (assets, index.php itself)
 RewriteCond %{REQUEST_FILENAME} -f [OR]
 RewriteCond %{REQUEST_FILENAME} -d
 RewriteRule ^ - [L]

 # /name.php?x=y -> /index.php?route=name&x=y
 # (matches regardless of subdirectory depth, e.g. /projects/7/login.php)
 RewriteCond %{REQUEST_URI} \.php$
 RewriteRule ^([A-Za-z0-9_]+)\.php$ index.php?route=$1 [QSA,L]

 # Pretty REST-ish paths: everything else that isn't a real file/dir
 # and isn't under assets/ or index.php itself gets forwarded to the
 # front controller with the full matched path in _path, preserving
 # any existing query string (?tab=..., ?export=csv, etc).
 RewriteCond %{REQUEST_URI} !^/(assets|index\.php)
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteRule ^(.*)$ index.php?_path=$1 [QSA,L]
</IfModule>

# Block direct access to application source, logs, and uploads
<IfModule mod_alias.c>
 RedirectMatch 403 ^/app/.*$
 RedirectMatch 403 ^/logs/.*$
 RedirectMatch 403 ^/uploads/.*$
</IfModule>

<FilesMatch "\.(sql|md|log)$">
 Require all denied
</FilesMatch>
