# =================================================
# IMAGEFIX.SITE - MAIN HTACCESS
# =================================================

RewriteEngine On
RewriteBase /

# =================================================
# CANONICAL DOMAIN
# Force HTTPS + WWW to NON-WWW
# =================================================

RewriteCond %{HTTPS} !=on [OR]
RewriteCond %{HTTP_HOST} ^www\.imagefix\.site$ [NC]
RewriteRule ^(.*)$ https://imagefix.site/$1 [R=301,L]

# =================================================
# REMOVE TRAILING SLASH
# Keep real directories unchanged
# =================================================

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/$
RewriteRule ^(.+)/$ /$1 [R=301,L]

# =================================================
# TOOLS
# =================================================

# Direct access to existing PHP tool files
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^tools/([a-zA-Z0-9-]+)\.php$ tools/$1.php [L,QSA]

# Tool URL without .php extension
# Example: /tools/jpg-to-pdf
RewriteCond %{DOCUMENT_ROOT}/tools/$1.php -f
RewriteRule ^tools/([a-zA-Z0-9-]+)$ tools/$1.php [L,QSA]

# Database-driven tool pages
# Example: /tools/usa-passport-photo
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^tools/([a-zA-Z0-9-]+)/?$ tools/view.php?slug=$1 [L,QSA]

# =================================================
# CATEGORY PAGES
# =================================================

RewriteRule ^category/([a-zA-Z0-9-]+)/?$ category.php?slug=$1 [L,QSA]

# =================================================
# BLOG PAGES
# =================================================

RewriteRule ^blog/category/([a-zA-Z0-9-]+)/?$ blog/category.php?slug=$1 [L,QSA]
RewriteRule ^blog/([a-zA-Z0-9-]+)/?$ blog/single.php?slug=$1 [L,QSA]

# =================================================
# REGULAR PAGES
# =================================================

RewriteRule ^pricing/?$ pricing.php [L,QSA]
RewriteRule ^contact/?$ contact.php [L,QSA]
RewriteRule ^login/?$ login.php [L,QSA]
RewriteRule ^register/?$ register.php [L,QSA]
RewriteRule ^logout/?$ logout.php [L,QSA]
RewriteRule ^verify-email/?$ verify-email.php [L,QSA]
RewriteRule ^reset-password/?$ reset-password.php [L,QSA]
RewriteRule ^forgot-password/?$ forgot-password.php [L,QSA]
RewriteRule ^tools/?$ tools.php [L,QSA]
RewriteRule ^blog/?$ blog/index.php [L,QSA]

# =================================================
# DYNAMIC CMS PAGES
# =================================================

RewriteRule ^page/([a-zA-Z0-9-]+)/?$ page.php?slug=$1 [L,QSA]

# =================================================
# BACKWARD COMPATIBILITY
# =================================================

RewriteRule ^about/?$ page.php?slug=about [L,QSA]
RewriteRule ^privacy/?$ page.php?slug=privacy [L,QSA]
RewriteRule ^terms/?$ page.php?slug=terms [L,QSA]
RewriteRule ^dmca/?$ page.php?slug=dmca [L,QSA]

# =================================================
# SECURITY
# =================================================

# Prevent directory listing
Options -Indexes

# Protect hidden files
<FilesMatch "^\.">
    Order allow,deny
    Deny from all
</FilesMatch>

# Protect sensitive files
<FilesMatch "(^|/)(config\.php|database\.sql|\.htaccess|\.git|error_log)$">
    Order allow,deny
    Deny from all
</FilesMatch>

# Block direct access to includes
RewriteRule ^includes/ - [F,L,NC]

# Block direct access to vendor
RewriteRule ^vendor/ - [F,L,NC]

# =================================================
# PERFORMANCE - GZIP / DEFLATE
# =================================================

<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/html
    AddOutputFilterByType DEFLATE text/plain
    AddOutputFilterByType DEFLATE text/xml
    AddOutputFilterByType DEFLATE text/css
    AddOutputFilterByType DEFLATE text/javascript
    AddOutputFilterByType DEFLATE application/javascript
    AddOutputFilterByType DEFLATE application/json
    AddOutputFilterByType DEFLATE application/xml
    AddOutputFilterByType DEFLATE application/xhtml+xml
    AddOutputFilterByType DEFLATE image/svg+xml
</IfModule>

# =================================================
# PERFORMANCE - BROWSER CACHING
# =================================================

<IfModule mod_expires.c>

    ExpiresActive On

    # Images
    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 image/webp "access plus 1 year"
    ExpiresByType image/svg+xml "access plus 1 year"
    ExpiresByType image/x-icon "access plus 1 year"

    # CSS
    ExpiresByType text/css "access plus 1 month"

    # JavaScript
    ExpiresByType application/javascript "access plus 1 month"
    ExpiresByType text/javascript "access plus 1 month"

    # Fonts
    ExpiresByType font/woff "access plus 1 year"
    ExpiresByType font/woff2 "access plus 1 year"
    ExpiresByType application/font-woff "access plus 1 year"
    ExpiresByType application/font-woff2 "access plus 1 year"

</IfModule>

# =================================================
# ERROR DOCUMENTS
# =================================================

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