# ZanCMS UI · Apache 生产规则（ui.zancms.cn）
# 用法：覆盖站点根目录的 .htaccess；不要用于本地 8087 开发站。

DirectoryIndex index.html
ErrorDocument 404 /404.html
AddDefaultCharset UTF-8
Options -Indexes

# 纯静态站，只允许读请求
<LimitExcept GET HEAD>
  Require all denied
</LimitExcept>

<IfModule mod_mime.c>
  RemoveHandler .php .phtml .phar .php3 .php4 .php5 .php7 .php8
  RemoveType .php .phtml .phar .php3 .php4 .php5 .php7 .php8
</IfModule>

<IfModule mod_rewrite.c>
  RewriteEngine On

  # 证书续期路径不跳转（IP 与域名都放行）
  RewriteCond %{REQUEST_URI} !^/\.well-known/acme-challenge/
  # 只保留规范域名，避免 IP / 别名进入
  RewriteCond %{HTTP_HOST} !^ui\.zancms\.cn$ [NC]
  RewriteRule ^ https://ui.zancms.cn%{REQUEST_URI} [L,R=301]

  # 证书续期路径不跳转
  RewriteCond %{REQUEST_URI} !^/\.well-known/acme-challenge/
  RewriteCond %{HTTPS} !=on
  RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>

# 降低扫描噪音：拒绝内部路径、构建文件与开发截图（不是源码保密）
<IfModule mod_rewrite.c>
  RewriteEngine On

  # 只允许读取方法
  RewriteCond %{REQUEST_METHOD} !^(GET|HEAD)$
  RewriteRule ^ - [F,L]

  # 轻量 WAF：常见扫描器 UA
  RewriteCond %{HTTP_USER_AGENT} (?:sqlmap|nikto|nmap|masscan|zgrab|nuclei|acunetix|nessus|openvas|w3af|dirbuster|gobuster|ffuf|wfuzz|hydra) [NC]
  RewriteRule ^ - [F,L]

  # 常见后台 / 敏感文件探测
  RewriteRule ^(?:wp-admin|wp-login\.php|xmlrpc\.php|phpmyadmin|adminer(?:\.php)?|\.env|\.git|\.svn|\.DS_Store)(?:/|$) - [F,L,NC]

  # 路径穿越、空字节、编码绕过
  RewriteCond %{REQUEST_URI} (?:\.\./|\.\.\\|%2e%2e|%252e|%00|%0d|%0a) [NC]
  RewriteRule ^ - [F,L]

  # 敏感文件读取与伪协议
  RewriteCond %{REQUEST_URI} (?:etc(?:/|%2f)passwd|proc(?:/|%2f)self|win\.ini|boot\.ini|php(?::|%3a)(?://|%2f%2f)|data(?::|%3a)(?://|%2f%2f)|expect(?::|%3a)(?://|%2f%2f)) [NC]
  RewriteRule ^ - [F,L]

  # 查询串中的 SQLi / XSS / 命令注入探测
  RewriteCond %{QUERY_STRING} (?:union(?:%20|\+)+select|information_schema|sleep\(\s*\d|benchmark\(|%3cscript|<script|javascript:|onerror(?:%3d|=)|(?:%27|')?\s*or(?:%20|\+)+1(?:%3d|=)1) [NC]
  RewriteRule ^ - [F,L]

  RewriteCond %{QUERY_STRING} (?:;|%3b)(?:%20|\+)?(?:cat|wget|curl|bash|sh|nc)(?:%20|\+) [NC]
  RewriteRule ^ - [F,L]

  # 查询串里的路径穿越与敏感读取
  RewriteCond %{QUERY_STRING} (?:\.\.(?:/|%2f|%5c)|%2e%2e|%252e|%00|%0d|%0a) [NC]
  RewriteRule ^ - [F,L]
  RewriteCond %{QUERY_STRING} (?:etc(?:/|%2f)passwd|proc(?:/|%2f)self|win\.ini|php(?::|%3a)(?://|%2f%2f)|data(?::|%3a)(?://|%2f%2f)|expect(?::|%3a)(?://|%2f%2f)) [NC]
  RewriteRule ^ - [F,L]

  # 超长 URL / 查询串
  RewriteCond %{REQUEST_URI} ^.{2049,}
  RewriteRule ^ - [F,L]
  RewriteCond %{QUERY_STRING} ^.{1025,}
  RewriteRule ^ - [F,L]

  RewriteRule ^(?:src|tools|deploy|shots)(?:/|$) - [F,L,NC]
  RewriteRule ^(?:package\.json|README\.md)$ - [F,L,NC]
</IfModule>

<FilesMatch "\.(php|phtml|phar|php[0-9]?)$">
  Require all denied
</FilesMatch>

<Files ".user.ini">
  Require all denied
</Files>

<Files "LICENSE">
  ForceType text/plain
  AddDefaultCharset UTF-8
</Files>

<IfModule mod_headers.c>
  Header always set X-Content-Type-Options "nosniff"
  Header always set Referrer-Policy "strict-origin-when-cross-origin"
  Header always set Permissions-Policy "camera=(), microphone=(), geolocation=()"
  Header always set X-Frame-Options "DENY"
  Header always set Content-Security-Policy "default-src 'self'; base-uri 'self'; object-src 'none'; frame-ancestors 'none'; frame-src 'none'; img-src 'self' data: blob:; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline'; font-src 'self' data:; connect-src 'self'; form-action 'self'; manifest-src 'self'; worker-src 'none'"
  Header always set X-Permitted-Cross-Domain-Policies "none"
  Header always set Strict-Transport-Security "max-age=31536000" env=HTTPS

  <FilesMatch "\.(html|xml|txt)$">
    Header set Cache-Control "no-cache, must-revalidate"
  </FilesMatch>
  <FilesMatch "\.(css|js|png|jpg|jpeg|gif|webp|svg|ico|woff2)$">
    Header set Cache-Control "public, max-age=604800"
  </FilesMatch>
  <FilesMatch "\.zip$">
    Header set Cache-Control "public, max-age=3600"
  </FilesMatch>
</IfModule>

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

<IfModule mod_mime.c>
  AddType image/svg+xml .svg
  AddType font/woff2 .woff2
</IfModule>

<IfModule mod_expires.c>
  ExpiresActive On
  ExpiresByType text/css "access plus 7 days"
  ExpiresByType application/javascript "access plus 7 days"
  ExpiresByType image/png "access plus 7 days"
  ExpiresByType image/jpeg "access plus 7 days"
  ExpiresByType image/svg+xml "access plus 7 days"
  ExpiresByType application/zip "access plus 1 hour"
  ExpiresByType text/html "access plus 0 seconds"
</IfModule>


