RewriteEngine On
Options -Indexes

# -------------------------------------------------------
# 1. Catalog & Product Special Routes
# -------------------------------------------------------

# Maps /catalog/ to product-catelog.php
RewriteRule ^catalog/?$ product-catelog.php [L,QSA]

# Maps /catalog/hardware/ etc.
RewriteRule ^catalog/([^/]+)/?$ product-catelog.php?category=$1 [L,QSA]

# Maps /catalog with query parameters to product-catelog.php
RewriteCond %{REQUEST_URI} ^/catalog/? [NC]
RewriteRule ^catalog/?$ product-catelog.php [L,QSA]

# Maps /product/15-item-slug to product-detail.php?product_id=15
RewriteRule ^product/([0-9]+)-([^/]+)/?$ product-detail.php?product_id=$1 [L,QSA]

# Maps /item-slug to product-detail.php?slug=item-slug
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php !-f
RewriteRule ^([^/]+)/?$ product-detail.php?slug=$1 [L,QSA]

# Maps /brand/brand-slug to brand.php?brand_slug=brand-slug
RewriteRule ^brand/([^/]+)/?$ brand.php?brand_slug=$1 [L,QSA]

# Maps /model/model-slug to model.php?model_slug=model-slug
RewriteRule ^model/([^/]+)/?$ model.php?model_slug=$1 [L,QSA]

# Maps /compatibility/compatibility-slug to compatibility.php?compatibility_slug=compatibility-slug
RewriteRule ^compatibility/([^/]+)/?$ compatibility.php?compatibility_slug=$1 [L,QSA]

# Maps sitemap.xml to sitemap.xml.php
RewriteRule ^sitemap\.xml/?$ sitemap.xml.php [L,QSA]
RewriteRule ^sitemap-([^.]+)\.xml/?$ sitemap-$1.xml.php [L,QSA]

# -------------------------------------------------------
# 1.5 Special Routes for directory/file name collisions
# -------------------------------------------------------
# Maps /cart to cart.php (preventing directory index)
RewriteRule ^cart/?$ cart.php [L,QSA]

# Maps /profile to profile.php (preventing directory index)
RewriteRule ^profile/?$ profile.php [L,QSA]

# Maps /order-view to order-view.php (preventing directory index)
RewriteRule ^order-view/?$ order-view.php [L,QSA]

# Maps /wishlist to wishlist.php (preventing wishlist/ directory index)
RewriteRule ^wishlist/?$ wishlist.php [L,QSA]

# Maps /wallet to wallet.php (preventing wallet/ directory index)
RewriteRule ^wallet/?$ wallet.php [L,QSA]

# -------------------------------------------------------
# 2. Global: Redirect .php URLs to clean URLs (301)
#    Only for GET requests to preserve POST body
# -------------------------------------------------------
RewriteCond %{REQUEST_METHOD} GET
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+([^.]+)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,L]

# -------------------------------------------------------
# 3. Internally serve .php when clean URL is requested
#    e.g. /about -> about.php (no redirect loop)
# -------------------------------------------------------
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L,QSA]

# 5. Security - Prevent access to sensitive files
<Files ~ "^\.">
    Order allow,deny
    Deny from all
</Files>

# Block access to config files
<Files ~ "\.(ini|log|conf|cfg)$">
    Order allow,deny
    Deny from all
</Files>

# 6. Set default charset
AddDefaultCharset UTF-8

# 7. Gzip compression
<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/plain
    AddOutputFilterByType DEFLATE text/html
    AddOutputFilterByType DEFLATE text/xml
    AddOutputFilterByType DEFLATE text/css
    AddOutputFilterByType DEFLATE application/xml
    AddOutputFilterByType DEFLATE application/xhtml+xml
    AddOutputFilterByType DEFLATE application/rss+xml
    AddOutputFilterByType DEFLATE application/javascript
    AddOutputFilterByType DEFLATE application/x-javascript
</IfModule>

# 8. Set expires headers for caching
<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresByType image/jpg "access plus 1 year"
    ExpiresByType image/jpeg "access plus 1 year"
    ExpiresByType image/gif "access plus 1 year"
    ExpiresByType image/png "access plus 1 year"
    ExpiresByType text/css "access plus 1 month"
    ExpiresByType application/javascript "access plus 1 month"
    ExpiresByType application/x-javascript "access plus 1 month"
</IfModule>
