# Apache .htaccess Configuration
# 4KWallpaperZ - www.4kwallpaperz.com

# Enable Rewrite Engine
RewriteEngine On
RewriteBase /

# ================================
# FORCE HTTPS & WWW (PRODUCTION)
# ================================

# Force HTTPS
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# Force WWW
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# ================================
# SECURITY RULES
# ================================

# Prevent directory browsing
Options -Indexes

# Prevent access to includes directories
RewriteRule ^includes/ - [F,L]
RewriteRule ^secure_admin_x9K2mP/includes/ - [F,L]

# Prevent access to configuration files
<FilesMatch "^(config\.php|\.env|\.htaccess|\.htpasswd)$">
    Order Allow,Deny
    Deny from all
</FilesMatch>

# Prevent access to SQL and backup files
<FilesMatch "\.(sql|bak|backup|log)$">
    Order Allow,Deny
    Deny from all
</FilesMatch>

# Protect sensitive files
<FilesMatch "^(composer\.json|composer\.lock|package\.json|package-lock\.json)$">
    Order Allow,Deny
    Deny from all
</FilesMatch>

# ================================
# SEO-FRIENDLY URL REWRITING
# ================================

# Robots.txt: /robots.txt (IMPORTANT - Add this FIRST before sitemap)
RewriteRule ^robots\.txt$ robots.php [L]

# Sitemap: /sitemap.xml
RewriteRule ^sitemap\.xml$ sitemap.php [L]

# Wallpaper detail pages: /wallpaper/slug-name
RewriteRule ^wallpaper/([a-z0-9-]+)/?$ wallpaper/index.php?slug=$1 [L,QSA]

# Category pages: /category/category-slug
RewriteRule ^category/([a-z0-9-]+)/?$ category/index.php?slug=$1 [L,QSA]

# All Collections page: /collections
RewriteRule ^collections/?$ collections/index.php [L,QSA]

# Single Collection page: /collection/collection-slug
RewriteRule ^collection/([a-z0-9-]+)/?$ collection/index.php?slug=$1 [L,QSA]

# Search page: /search?q=keyword
RewriteRule ^search/?$ search/index.php [L,QSA]

# ================================
# LEGAL PAGES
# ================================

RewriteRule ^about/?$ pages/about.php [L]
RewriteRule ^contact/?$ pages/contact.php [L]
RewriteRule ^privacy/?$ pages/privacy.php [L]
RewriteRule ^terms/?$ pages/terms.php [L]
RewriteRule ^dmca/?$ pages/dmca.php [L]

# ================================
# CACHING & PERFORMANCE
# ================================

# Enable browser caching
<IfModule mod_expires.c>
    ExpiresActive On
    
    # Images
    ExpiresByType image/jpeg "access plus 1 year"
    ExpiresByType image/jpg "access plus 1 year"
    ExpiresByType image/png "access plus 1 year"
    ExpiresByType image/gif "access plus 1 year"
    ExpiresByType image/webp "access plus 1 year"
    
    # CSS and JavaScript
    ExpiresByType text/css "access plus 1 month"
    ExpiresByType application/javascript "access plus 1 month"
    ExpiresByType text/javascript "access plus 1 month"
    
    # Fonts
    ExpiresByType font/woff2 "access plus 1 year"
    ExpiresByType font/woff "access plus 1 year"
    ExpiresByType font/ttf "access plus 1 year"
    ExpiresByType font/otf "access plus 1 year"
    
    # HTML and JSON
    ExpiresByType text/html "access plus 0 seconds"
    ExpiresByType application/json "access plus 0 seconds"
</IfModule>

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

# ================================
# CACHE CONTROL
# ================================

# Cache static assets
<IfModule mod_headers.c>
    # Cache images for 1 year
    <FilesMatch "\.(jpg|jpeg|png|gif|webp|svg|ico)$">
        Header set Cache-Control "max-age=31536000, public"
    </FilesMatch>
    
    # Cache CSS and JS for 1 month
    <FilesMatch "\.(css|js)$">
        Header set Cache-Control "max-age=2592000, public"
    </FilesMatch>
    
    # Don't cache HTML
    <FilesMatch "\.(html|php)$">
        Header set Cache-Control "no-cache, no-store, must-revalidate"
        Header set Pragma "no-cache"
        Header set Expires 0
    </FilesMatch>
</IfModule>

# ================================
# SECURITY HEADERS
# ================================

<IfModule mod_headers.c>
    # Prevent clickjacking
    Header always set X-Frame-Options "SAMEORIGIN"
    
    # Prevent MIME type sniffing
    Header always set X-Content-Type-Options "nosniff"
    
    # Enable XSS protection
    Header always set X-XSS-Protection "1; mode=block"
    
    # Referrer Policy
    Header always set Referrer-Policy "strict-origin-when-cross-origin"
    
    # Remove server signature
    Header unset Server
    Header unset X-Powered-By
</IfModule>

# ================================
# CUSTOM ERROR PAGES
# ================================

ErrorDocument 404 /404.php
ErrorDocument 403 /403.php
ErrorDocument 500 /500.php

# ================================
# PHP SETTINGS (Optional)
# ================================

<IfModule mod_php7.c>
    php_value upload_max_filesize 10M
    php_value post_max_size 12M
    php_value max_execution_time 300
    php_value max_input_time 300
</IfModule>

# ================================
# PREVENT ACCESS TO HIDDEN FILES
# ================================

<FilesMatch "^\.">
    Order Allow,Deny
    Deny from all
</FilesMatch>
