Options -MultiViews
RewriteEngine On
RewriteBase /

# Redirect common root site requests accidentally scoped under /account/ back to the root
RewriteRule ^account/(products|about|contact|store-locator|track-order|cart|checkout|help-center|business-solutions)/?$ /$1/ [R=301,L]


# A. EXTERNAL REDIRECTS (Changes the URL in browser's address bar)

# 1. Redirect trailing slash to non-trailing slash for extensionless PHP files
# This ensures relative URLs and assets resolve correctly under their parent directories
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+)/$ /$1 [R=301,L]

# 2. Redirect direct requests for *.php to extensionless clean URLs
# We check THE_REQUEST to avoid infinite redirect loops with internal rewrites
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^.]+)\.php\s [NC]
RewriteRule ^([^.]+)\.php$ /$1 [R=301,L]


# B. INTERNAL REWRITES (Serves content without changing browser URL)

# 3. Stop rewriting for actual physical directories and files
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]

# 3.b Route admin product edit by slug: /admin/products/edit/some-slug -> admin/products.php?edit_slug=some-slug
RewriteRule ^admin/products/edit/([^/]+)/?$ admin/products.php?edit_slug=$1 [L,QSA]

# 3.c Route admin product add: /admin/products/add -> admin/products.php?add=1
RewriteRule ^admin/products/add/?$ admin/products.php?add=1 [L,QSA]

# 3.d Route admin product delete by slug: /admin/products/delete/some-slug -> admin/products.php?delete_slug=some-slug
RewriteRule ^admin/products/delete/([^/]+)/?$ admin/products.php?delete_slug=$1 [L,QSA]

# Route admin sample product edit by slug: /admin/sample-products/edit/some-slug -> admin/sample_products.php?edit_slug=some-slug
RewriteRule ^admin/sample-products/edit/([^/]+)/?$ admin/sample_products.php?edit_slug=$1 [L,QSA]

# Route admin sample product add: /admin/sample-products/add -> admin/sample_products.php?add=1
RewriteRule ^admin/sample-products/add/?$ admin/sample_products.php?add=1 [L,QSA]

# Route admin sample product delete by slug: /admin/sample-products/delete/some-slug -> admin/sample_products.php?delete_slug=some-slug
RewriteRule ^admin/sample-products/delete/([^/]+)/?$ admin/sample_products.php?delete_slug=$1 [L,QSA]

# 3.e Route admin categories actions: add, edit, delete by slug
RewriteRule ^admin/categories/add/?$ admin/categories.php?add=1 [L,QSA]
RewriteRule ^admin/categories/edit/([^/]+)/?$ admin/categories.php?edit_slug=$1 [L,QSA]
RewriteRule ^admin/categories/delete/([^/]+)/?$ admin/categories.php?delete_slug=$1 [L,QSA]

# Route admin subcategories actions: add, edit, delete by slug
RewriteRule ^admin/subcategories/add/?$ admin/subcategories.php?add=1 [L,QSA]
RewriteRule ^admin/subcategories/edit/([^/]+)/?$ admin/subcategories.php?edit_slug=$1 [L,QSA]
RewriteRule ^admin/subcategories/delete/([^/]+)/?$ admin/subcategories.php?delete_slug=$1 [L,QSA]

# Route admin tags actions: add, edit, delete by slug
RewriteRule ^admin/tags/add/?$ admin/tags.php?add=1 [L,QSA]
RewriteRule ^admin/tags/edit/([^/]+)/?$ admin/tags.php?edit_slug=$1 [L,QSA]
RewriteRule ^admin/tags/delete/([^/]+)/?$ admin/tags.php?delete_slug=$1 [L,QSA]

# 3.f Route admin coupons actions: add, edit, delete by ID
RewriteRule ^admin/coupons/add/?$ admin/coupons.php?add=1 [L,QSA]
RewriteRule ^admin/coupons/edit/([0-9]+)/?$ admin/coupons.php?edit=$1 [L,QSA]
RewriteRule ^admin/coupons/delete/([0-9]+)/?$ admin/coupons.php?delete=$1 [L,QSA]

# 4. Route subcategories (clean nested URLs, supporting double slash tags)
RewriteRule ^products/categories/([^/]+)/subcategories/([^/]+)/+tag/([^/]+)/sort/([^/]+)/?$ products.php?category=$1&subcategory=$2&tag=$3&sort=$4 [L,QSA]
RewriteRule ^products/categories/([^/]+)/subcategories/([^/]+)/+tag/([^/]+)/?$ products.php?category=$1&subcategory=$2&tag=$3 [L,QSA]
RewriteRule ^products/categories/([^/]+)/subcategories/([^/]+)/sort/([^/]+)/?$ products.php?category=$1&subcategory=$2&sort=$3 [L,QSA]
RewriteRule ^products/categories/([^/]+)/subcategories/([^/]+)/?$ products.php?category=$1&subcategory=$2 [L,QSA]

# 4. Route categories (clean nested URLs)
RewriteRule ^products/categories/([^/]+)/tag/([^/]+)/sort/([^/]+)/?$ products.php?category=$1&tag=$2&sort=$3 [L,QSA]
RewriteRule ^products/categories/([^/]+)/tag/([^/]+)/?$ products.php?category=$1&tag=$2 [L,QSA]
RewriteRule ^products/categories/([^/]+)/sort/([^/]+)/?$ products.php?category=$1&sort=$2 [L,QSA]
RewriteRule ^products/categories/([^/]+)/?$ products.php?category=$1 [L,QSA]

# 5. Route product details: /product-details/premium-polo-tshirt -> product-details.php?slug=premium-polo-tshirt
RewriteRule ^product-details/([^/]+)/?$ product-details.php?slug=$1 [L,QSA]

# 5.c Route products search (clean nested URLs)
RewriteRule ^products/search/([^/]+)/tag/([^/]+)/sort/([^/]+)/?$ products.php?search=$1&tag=$2&sort=$3 [L,QSA]
RewriteRule ^products/search/([^/]+)/tag/([^/]+)/?$ products.php?search=$1&tag=$2 [L,QSA]
RewriteRule ^products/search/([^/]+)/sort/([^/]+)/?$ products.php?search=$1&sort=$2 [L,QSA]
RewriteRule ^products/search/([^/]+)/?$ products.php?search=$1 [L,QSA]
RewriteRule ^products/search/?$ products.php [L,QSA]

# 5.d Route products generic tags/sorts
RewriteRule ^products/tag/([^/]+)/sort/([^/]+)/?$ products.php?tag=$1&sort=$2 [L,QSA]
RewriteRule ^products/tag/([^/]+)/?$ products.php?tag=$1 [L,QSA]
RewriteRule ^products/sort/([^/]+)/?$ products.php?sort=$1 [L,QSA]

# 5.b Route store-locator and search endpoints
RewriteRule ^store-locator/?$ store-locator.php [L,QSA]
RewriteRule ^store-locator/search/?$ store-locator.php [L,QSA]
RewriteRule ^store-locator/search-([^/]+)/?$ store-locator.php [L,QSA]

# 6. Serve extensionless PHP files at any folder depth (no trailing slash)
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)$ $1.php [L,QSA]
