# ============================================================
# ZhiCms 安全加固（Apache 版，适配 AllowOverride All 的站点）
# 与 Nginx 的 nginx.conf（server 块版）/ nginx.htaccess（include 精简版）策略保持一致：
#   - 伪静态 rewrite 到 index.php（交由框架 rule.php 解析业务路由）
#   - 禁止 Web 访问敏感目录（data / runtime / vendor / .git / .svn）
#   - 禁止下载敏感文件类型
#   - 防挂马：默认禁止所有 .php 执行，仅白名单放行 index.php / sitemap.php
# ============================================================

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

# 1) 伪静态（必需）：不存在的文件/目录统一转发到 index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?r=$1 [QSA,L]

# 2) 禁止 Web 访问敏感目录（返回 403）
RewriteRule ^data/ - [F,L]
RewriteRule ^runtime/ - [F,L]
RewriteRule ^vendor/ - [F,L]
RewriteRule ^\.git/ - [F,L]
RewriteRule ^\.svn/ - [F,L]
RewriteRule ^composer\.(json|lock)$ - [F,L]
</IfModule>

# 3) 防挂马核心规则：默认禁止【除白名单外】所有 .php 执行
#    仅放行 /index.php、/sitemap.php；其余 .php/.php5/.phtml/.pht/.phps 一律拒绝。
#    install.php / build-classmap.php / rebuild-classmap.php / classmap.php 等构建/安装脚本禁止 Web 访问。
<FilesMatch "(?i)^(?!index\.php|sitemap\.php).*\.(php|php5|phtml|pht|phps)$">
    <IfModule mod_authz_core.c>
        Require all denied
    </IfModule>
    <IfModule !mod_authz_core.c>
        Order allow,deny
        Deny from all
    </IfModule>
</FilesMatch>

# 4) 禁止直接下载敏感文件类型（配置、备份、SQL、日志、密钥、锁文件）
<FilesMatch "(?i)\.(sql|log|bak|ini|lock|env|key|pem|crt)$">
    <IfModule mod_authz_core.c>
        Require all denied
    </IfModule>
    <IfModule !mod_authz_core.c>
        Order allow,deny
        Deny from all
    </IfModule>
</FilesMatch>
