# ============================================================
# 周师傅开锁网站 .htaccess 配置
# ============================================================

# 启用重写引擎
RewriteEngine On

# 404 错误页面
ErrorDocument 404 /404.php

# ============================================================
# 【重要】先注释掉 HTTPS 跳转，等 SSL 证书配置好再启用
# ============================================================
# RewriteCond %{HTTPS} off
# RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]

# ============================================================
# 文件访问控制
# ============================================================

# 1. 禁止访问以点开头的文件
<FilesMatch "^\.">
    Order allow,deny
    Deny from all
</FilesMatch>

# 2. 保护数据库文件
<FilesMatch "\.(db|sqlite|sqlite3)$">
    Order allow,deny
    Deny from all
</FilesMatch>

# 3. 保护日志文件
<FilesMatch "\.log$">
    Order allow,deny
    Deny from all
</FilesMatch>

# 4. 保护配置文件
<FilesMatch "\.(json|yml|yaml|ini|conf)$">
    Order allow,deny
    Deny from all
</FilesMatch>

# 5. 禁止访问核心 PHP 文件（防止直接执行）
<FilesMatch "(config|database|functions)\.php$">
    Order allow,deny
    Deny from all
</FilesMatch>

# ============================================================
# 安全头信息（仅在 Apache 支持 mod_headers 时启用）
# ============================================================
<IfModule mod_headers.c>
    Header set X-Frame-Options "SAMEORIGIN"
    Header set X-XSS-Protection "1; mode=block"
    Header set X-Content-Type-Options "nosniff"
</IfModule>

# ============================================================
# 缓存静态资源
# ============================================================
<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 image/webp "access plus 1 year"
    ExpiresByType text/css "access plus 1 month"
    ExpiresByType application/javascript "access plus 1 month"
    ExpiresByType font/woff2 "access plus 1 year"
</IfModule>

# ============================================================
# 压缩传输
# ============================================================
<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/html text/css text/plain text/xml
    AddOutputFilterByType DEFLATE application/javascript application/x-javascript
    AddOutputFilterByType DEFLATE application/json application/xml
</IfModule>

# ============================================================
# 禁止目录列表
# ============================================================
Options -Indexes