# ===== Apache 服务器安全加固 =====
# 1. 禁止直接访问配置文件（config.php 含数据库密码等敏感信息）
<Files "config.php">
    Order allow,deny
    Deny from all
</Files>

# 2. 禁止访问其他敏感文件类型
<FilesMatch "\.(sql|db|sqlite|log|md|lock|bak|env|ini|yml|pem|key|crt)$">
    Order allow,deny
    Deny from all
</FilesMatch>

# 3. 禁止列出目录内容
Options -Indexes

# ===== Apache 伪静态规则 =====
# 如果 .htaccess 不生效，Router 已内置 REQUEST_URI 解析，无需依赖本文件
# 但需要确保所有非真实文件的请求都转发给 index.php（见下方 FallbackResource）

# FallbackResource：即使 mod_rewrite 未加载，也能将 404 转发给 index.php
FallbackResource /index.php

# mod_rewrite 规则（如果 mod_rewrite 已启用，提供更精确的路由）
<IfModule mod_rewrite.c>
RewriteEngine On
Options +FollowSymLinks -MultiViews

# 禁止直接访问会员公共模板源码（static/member/ 下为含标签的源文件，仅由 PHP 读取渲染）
RewriteRule ^static/member/ - [F,L]

# 禁止直接访问应用源码（app/ 下为框架与插件源码，仅由 PHP 读取）
# 仅放行插件对外资源目录 assets/ 与插件自带端点 php/，其余一律 403
RewriteRule ^app/plugins/[^/]+/(assets|php)/ - [L]
RewriteRule ^app/ - [F,L]

# 禁止直接访问主题源码（template/ 下为含 {qbcms:}/{var:} 等模板标签的源文件，仅由 PHP 读取渲染）
# 仅放行 css/js/图片/字体/媒体等静态资源（各主题静态资源目录不统一，如 assets/css 与 css），其余一律 403
RewriteCond %{REQUEST_URI} !\.(css|js|mjs|map|png|jpg|jpeg|gif|webp|avif|svg|ico|bmp|woff|woff2|ttf|otf|eot|mp4|webm|mp3|pdf)$ [NC]
RewriteRule ^template/ - [F,L]

# 如果请求的是真实文件或目录，直接返回
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]

# 首页
RewriteRule ^$ index.php [L]

# 列表页: /list-1.html 或 /list/1
RewriteRule ^list-([0-9]+)\.html$ index.php?c=List&a=index&id=$1 [QSA,L]
RewriteRule ^list/([0-9]+)$ index.php?c=List&a=index&id=$1 [QSA,L]

# 文章页: /article-1.html 或 /article/1
RewriteRule ^article-([0-9]+)\.html$ index.php?c=Article&a=index&id=$1 [QSA,L]
RewriteRule ^article/([0-9]+)$ index.php?c=Article&a=index&id=$1 [QSA,L]

# 单页(别名): /page/alias/
RewriteRule ^page/([a-zA-Z0-9\-]+)/?$ index.php?c=Page&a=show&alias=$1 [QSA,L]

# 专题详情页(别名): /topic/alias/
RewriteRule ^topic/([a-zA-Z0-9\-]+)/?$ index.php?c=Topic&a=show&alias=$1 [QSA,L]

# 专题列表页: /topic (无别名)
RewriteRule ^topic/?$ index.php?c=Topic&a=index [QSA,L]

# 标签云页: /tag (无标签名)
RewriteRule ^tag/?$ index.php?c=Tag&a=cloud [QSA,L]

# 标签文章列表页: /tag/标签名/ (支持中文)
RewriteRule ^tag/(.+?)/?$ index.php?c=Tag&a=lists&name=$1 [QSA,L]

# 会员中心
RewriteRule ^member/login$      index.php?c=Member&a=login      [QSA,L]
RewriteRule ^member/register$   index.php?c=Member&a=register  [QSA,L]
RewriteRule ^member/logout$     index.php?c=Member&a=logout    [QSA,L]
RewriteRule ^member/dashboard$  index.php?c=Member&a=dashboard [QSA,L]
RewriteRule ^member/profile$    index.php?c=Member&a=profile   [QSA,L]
RewriteRule ^member/comments$   index.php?c=Member&a=myComments   [QSA,L]
RewriteRule ^member/guestbook$  index.php?c=Member&a=myGuestbook   [QSA,L]

# 投稿中心
RewriteRule ^contribution/list$   index.php?c=Contribution&a=list   [QSA,L]
RewriteRule ^contribution/create$ index.php?c=Contribution&a=create [QSA,L]

# 栏目页(别名): /alias/
# 放在通用规则之前，避免被 /xxx-1.html 规则匹配
# 排除已知控制器路径
RewriteCond %{REQUEST_URI} !^/(admin|install|uploads|template|assets|captcha\.php|topic|tag)
RewriteRule ^([a-zA-Z0-9\-]+)/?$ index.php?c=List&a=index&alias=$1 [QSA,L]

# 默认路由: /xxx-1.html
RewriteRule ^([a-zA-Z]+)-([0-9]+)\.html$ index.php?c=$1&a=index&id=$2 [QSA,L]
RewriteRule ^([a-zA-Z]+)/([0-9]+)$ index.php?c=$1&a=index&id=$2 [QSA,L]

# 单页模式: /guestbook.html
RewriteRule ^([a-zA-Z]+)\.html$ index.php?c=$1&a=index [QSA,L]

# 所有其他请求交给 index.php（Router 会从 REQUEST_URI 解析）
RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>
