# Prompt Studio - Friendly URLs & Subdirectory Routing
# @version 3.0.0 - Enhanced for subdirectory installation

<IfModule mod_rewrite.c>
    RewriteEngine On
    
    # === Dynamic Base Path Detection ===
    # This works automatically whether the script is installed at:
    # - Domain root: https://example.com/
    # - Subdirectory: https://example.com/prompt/
    # No manual configuration needed.
    
    # Do not rewrite existing files or directories (assets, images, etc.)
    RewriteCond %{REQUEST_FILENAME} -f [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^ - [L]
    
    # === URL Routing Rules ===
    
    # 1. Language-prefixed page: /ar/image OR /en/blog
    # Maps to: index.php?lang=$1&page=$2
    RewriteRule ^(ar|en)/([^/]+)/?$ index.php?lang=$1&page=$2 [QSA,L]
    
    # 2. Language home: /ar OR /en
    # Maps to: index.php?lang=$1&page=home
    RewriteRule ^(ar|en)/?$ index.php?lang=$1&page=home [QSA,L]
    
    # 3. Root home: / OR /index
    # Maps to: index.php?page=home
    RewriteRule ^/?$ index.php?page=home [QSA,L]
    
    # 4. Sitemap XML: /sitemap.xml
    RewriteRule ^sitemap\.xml$ sitemap-xml.php [L]
    
    # 5. Robots.txt (serve static file)
    # Already served as static file, no rewrite needed
    
    # 6. Old-style query string URLs (?page=X) still work — no rewrite needed
    # because index.php is the default DirectoryIndex.
    
    # 7. Fallback: any non-existent path becomes a blog article slug
    # Example: /ar/my-article-slug -> index.php?lang=ar&page=my-article-slug
    # (PHP will detect it's an article and load article.php)
    # This is already handled by rule #1 above.
</IfModule>

# === Directory Index ===
DirectoryIndex index.php index.html

# === Caching for Performance ===
<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresByType text/css "access plus 1 month"
    ExpiresByType application/javascript "access plus 1 month"
    ExpiresByType text/javascript "access plus 1 month"
    ExpiresByType image/png "access plus 1 month"
    ExpiresByType image/jpeg "access plus 1 month"
    ExpiresByType image/jpg "access plus 1 month"
    ExpiresByType image/gif "access plus 1 month"
    ExpiresByType image/webp "access plus 1 month"
    ExpiresByType image/svg+xml "access plus 1 month"
    ExpiresByType image/x-icon "access plus 1 year"
    ExpiresByType application/pdf "access plus 1 month"
    ExpiresByType font/woff "access plus 1 year"
    ExpiresByType font/woff2 "access plus 1 year"
    ExpiresByType application/font-woff "access plus 1 year"
    ExpiresByType application/vnd.ms-fontobject "access plus 1 year"
</IfModule>

# === Compression ===
<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/html text/plain text/css text/xml text/javascript
    AddOutputFilterByType DEFLATE application/javascript application/x-javascript application/json
    AddOutputFilterByType DEFLATE application/xml application/rss+xml image/svg+xml
</IfModule>

# === Security Headers ===
<IfModule mod_headers.c>
    Header set X-Content-Type-Options "nosniff"
    Header set X-Frame-Options "SAMEORIGIN"
    Header set X-XSS-Protection "1; mode=block"
    Header set Referrer-Policy "strict-origin-when-cross-origin"
    Header set Permissions-Policy "geolocation=(), microphone=(), camera=()"
    # Force HTTPS if enabled in settings (uncomment if needed)
    # Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains" env=HTTPS
</IfModule>

# === Block Access to Sensitive Files ===
<FilesMatch "^\.">
    Order allow,deny
    Deny from all
</FilesMatch>

# Block access to JSON data files (data directory)
<FilesMatch "\.(json)$">
    Order allow,deny
    Deny from all
</FilesMatch>

# Block access to backup files
<FilesMatch "\.(bak|obf\.bak)$">
    Order allow,deny
    Deny from all
</FilesMatch>

# Block access to specific sensitive directories
<IfModule mod_rewrite.c>
    RewriteRule ^data/ - [F,L]
    RewriteRule ^storage/ - [F,L]
    RewriteRule ^templates/ - [F,L]
</IfModule>

# === Character Encoding ===
AddDefaultCharset UTF-8
