# ========== 安全防护 ==========
# 禁止访问 app/ runtime/ 目录
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

RewriteRule ^app/ - [F,L]
RewriteRule ^runtime/ - [F,L]
RewriteRule ^document/ - [F,L]
RewriteRule ^tools/ - [F,L]
RewriteRule ^config/ - [F,L]
</IfModule>

# 禁止访问敏感文件类型
<FilesMatch "\.(md|txt|log|sql|bak|env|ini)$">
    <IfModule mod_authz_core.c>
        Require all denied
    </IfModule>
    <IfModule !mod_authz_core.c>
        Order deny,allow
        Deny from all
    </IfModule>
</FilesMatch>

# ========== 后台迁移 301 重定向 ==========
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

# 广告
RewriteRule ^ads\.php$ /admin/ad/list.php [R=301,L]
RewriteRule ^ad-edit\.php$ /admin/ad/edit.php [R=301,L]
RewriteRule ^ad-delete\.php$ /admin/ad/delete.php [R=301,L]

# 菜单
RewriteRule ^menus\.php$ /admin/menu/list.php [R=301,L]
RewriteRule ^menu-edit\.php$ /admin/menu/edit.php [R=301,L]
RewriteRule ^menu-delete\.php$ /admin/menu/delete.php [R=301,L]
RewriteRule ^menus-by-domain\.php$ /admin/menu/by-domain.php [R=301,L]

# 管理员
RewriteRule ^admin-users\.php$ /admin/admin-user/list.php [R=301,L]
RewriteRule ^admin-user-add\.php$ /admin/admin-user/add.php [R=301,L]
RewriteRule ^admin-user-edit\.php$ /admin/admin-user/edit.php [R=301,L]
RewriteRule ^admin-user-delete\.php$ /admin/admin-user/delete.php [R=301,L]
RewriteRule ^admin-user-toggle\.php$ /admin/admin-user/toggle.php [R=301,L]
RewriteRule ^admin-user-password\.php$ /admin/admin-user/password.php [R=301,L]
RewriteRule ^admin-user-logs\.php$ /admin/admin-user/logs.php [R=301,L]

# 标签
RewriteRule ^tag-data-rebuild\.php$ /admin/tag/data-rebuild.php [R=301,L]
RewriteRule ^tag-debug\.php$ /admin/tag/debug.php [R=301,L]
RewriteRule ^tag-slug-rebuild\.php$ /admin/tag/slug-rebuild.php [R=301,L]

# AI 模型
RewriteRule ^ai-models\.php$ /admin/ai-model/list.php [R=301,L]
RewriteRule ^ai-model-add\.php$ /admin/ai-model/add.php [R=301,L]
RewriteRule ^ai-model-edit\.php$ /admin/ai-model/edit.php [R=301,L]
RewriteRule ^ai-model-config\.php$ /admin/ai-model/config.php [R=301,L]
RewriteRule ^ai-model-test\.php$ /admin/ai-model/test.php [R=301,L]

# 模板套件
RewriteRule ^template-packs\.php$ /admin/template-pack/list.php [R=301,L]
RewriteRule ^template-pack-edit\.php$ /admin/template-pack/edit.php [R=301,L]
RewriteRule ^template-pack-preview\.php$ /admin/template-pack/preview.php [R=301,L]

# 组件
RewriteRule ^components\.php$ /admin/component/list.php [R=301,L]
RewriteRule ^component-edit\.php$ /admin/component/edit.php [R=301,L]
RewriteRule ^component-delete\.php$ /admin/component/delete.php [R=301,L]
RewriteRule ^component-visual-edit\.php$ /admin/component/visual-edit.php [R=301,L]

# 域名
RewriteRule ^domains\.php$ /admin/domain/list.php [R=301,L]
RewriteRule ^domain-add\.php$ /admin/domain/add.php [R=301,L]
RewriteRule ^domain-edit\.php$ /admin/domain/edit.php [R=301,L]
RewriteRule ^domain-delete\.php$ /admin/domain/delete.php [R=301,L]
RewriteRule ^domain-site-check\.php$ /admin/domain/site-check.php [R=301,L]
RewriteRule ^domain-static-publish\.php$ /admin/domain/static-publish.php [R=301,L]

# 内容
RewriteRule ^content\.php$ /admin/content/list.php [R=301,L]
RewriteRule ^content-add\.php$ /admin/content/add.php [R=301,L]
RewriteRule ^content-edit\.php$ /admin/content/edit.php [R=301,L]
RewriteRule ^content-delete\.php$ /admin/content/delete.php [R=301,L]
RewriteRule ^content-status\.php$ /admin/content/status.php [R=301,L]
RewriteRule ^content-preview\.php$ /admin/content/preview.php [R=301,L]
RewriteRule ^content-batch\.php$ /admin/content/batch.php [R=301,L]
RewriteRule ^content-static-publish\.php$ /admin/content/static-publish.php [R=301,L]
RewriteRule ^content-fix-cover-paths\.php$ /admin/content/fix-cover-paths.php [R=301,L]

# 站点构建
RewriteRule ^site-build\.php$ /admin/site-build/index.php [R=301,L]
RewriteRule ^site-build-run\.php$ /admin/site-build/run.php [R=301,L]
RewriteRule ^site-build-start\.php$ /admin/site-build/start.php [R=301,L]
RewriteRule ^site-build-task\.php$ /admin/site-build/task.php [R=301,L]

# 文章任务
RewriteRule ^article-tasks\.php$ /admin/article-task/list.php [R=301,L]
RewriteRule ^article-task-edit\.php$ /admin/article-task/edit.php [R=301,L]
RewriteRule ^article-task-delete\.php$ /admin/article-task/delete.php [R=301,L]
RewriteRule ^article-task-run\.php$ /admin/article-task/run.php [R=301,L]
RewriteRule ^article-task-status\.php$ /admin/article-task/status.php [R=301,L]

# 单页管理
RewriteRule ^spider-stats\.php$ /admin/spider-stats/index.php [R=301,L]
RewriteRule ^settings\.php$ /admin/settings/index.php [R=301,L]
RewriteRule ^help\.php$ /admin/help/index.php [R=301,L]
RewriteRule ^system-update\.php$ /admin/system-update/index.php [R=301,L]
RewriteRule ^admin\.php$ /admin/dashboard/index.php [R=301,L]

# API 端点
RewriteRule ^ai-generate-keywords\.php$ /admin/api/ai-generate-keywords.php [R=301,L]
RewriteRule ^ai-generate-menu-seo\.php$ /admin/api/ai-generate-menu-seo.php [R=301,L]
RewriteRule ^ai-generate-site-desc\.php$ /admin/api/ai-generate-site-desc.php [R=301,L]
RewriteRule ^ai-generate-site-names\.php$ /admin/api/ai-generate-site-names.php [R=301,L]
RewriteRule ^api-ai-template\.php$ /admin/api/ai-template.php [R=301,L]
RewriteRule ^api-ai-template-step\.php$ /admin/api/ai-template-step.php [R=301,L]
RewriteRule ^api-slug\.php$ /admin/api/slug.php [R=301,L]
RewriteRule ^content-batch-publish-api\.php$ /admin/api/content-batch-publish.php [R=301,L]
RewriteRule ^content-batch-draft-publish-api\.php$ /admin/api/content-batch-draft-publish.php [R=301,L]

# 编辑器 API
RewriteRule ^editor-ai-generate\.php$ /admin/api/editor-ai-generate.php [R=301,L]
RewriteRule ^editor-ai-generate-seo\.php$ /admin/api/editor-ai-generate-seo.php [R=301,L]
RewriteRule ^editor-generate-cover\.php$ /admin/api/editor-generate-cover.php [R=301,L]
RewriteRule ^editor-upload-image\.php$ /admin/api/editor-upload-image.php [R=301,L]

# CLI 脚本
RewriteRule ^article-task-runner\.php$ /admin/cli/article-task-runner.php [R=301,L]
RewriteRule ^article-task-runner-cli\.php$ /admin/cli/article-task-runner-cli.php [R=301,L]
RewriteRule ^tag-slug-rebuild-cli\.php$ /admin/cli/tag-slug-rebuild-cli.php [R=301,L]
</IfModule>

# ========== URL 重写 ==========
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]

# 命中域名专属静态（site/{host}/...）
RewriteCond %{HTTP_HOST} !=
RewriteCond %{DOCUMENT_ROOT}/site/%{HTTP_HOST}/%{REQUEST_URI} -f
RewriteRule ^ site/%{HTTP_HOST}/%{REQUEST_URI} [L]

# 首页：如存在 /site/{host}/index.html 则优先使用，否则交给 PHP 动态入口
RewriteCond %{REQUEST_URI} ^/$
RewriteCond %{HTTP_HOST} !=
RewriteCond %{DOCUMENT_ROOT}/site/%{HTTP_HOST}/index.html -f
RewriteRule ^ site/%{HTTP_HOST}/index.html [L]

# 文章详情页：/wps/wps-2.html
RewriteRule ^(.+)\.html$ index.php?slug=$1 [L,QSA]

# 首页分页：/page/2/
RewriteRule ^page/(\d+)/?$ index.php?slug=index/page/$1 [L,QSA]

# 文章聚合页：/articles/
RewriteRule ^articles/?$ index.php?slug=articles [L,QSA]

# 标签页：/tag/fareyingdui/
RewriteRule ^tag/([A-Za-z0-9\-_]+)/?$ index.php?slug=tag/$1 [L,QSA]

# 标签分页：/tag/fareyingdui/page/2/
RewriteRule ^tag/([A-Za-z0-9\-_]+)/page/(\d+)/?$ index.php?slug=tag/$1/page/$2 [L,QSA]

# 栏目页分页：/tiyu/page/2/
RewriteRule ^([A-Za-z0-9\-_]+)/page/(\d+)/?$ index.php?slug=$1/page/$2 [L,QSA]

# 栏目页：/tiyu/
RewriteRule ^([A-Za-z0-9\-_]+)/?$ index.php?slug=$1 [L,QSA]

# 首页兜底：未命中静态与规则时，统一回落到 PHP 动态入口
RewriteRule ^$ index.php [L,QSA]
</IfModule>
