<IfModule mod_rewrite.c>
    # 启用URL重写模块
    RewriteEngine On
    
    # 确保当前目录是web根目录
    RewriteBase /
# 允许本地打包工具直接处理自身请求，避免被后续站点规则改写成404
    RewriteRule ^\.master/qkcmszip\.php$ - [END,NC]

    RewriteCond %{REQUEST_URI} !^/\.master/ [NC]
    RewriteRule "(^|/)\.(?!well-known/)" - [F,L,NC]
    RewriteRule "(^|/)(config\.php|web\.config|composer\.(json|lock)|package(-lock)?\.json|yarn\.lock)$" - [F,L,NC]
    RewriteRule "\.(env|ini|sql|bak|old|dist|log)(\..*)?$" - [F,L,NC]
    
    # 禁止uploads目录脚本
    RewriteRule ^uploads/.*\.(php|phtml|pht|phar|cgi|pl|asp|aspx)$ - [F,L,NC]
    
    # favicon 路由到实际文件
    RewriteRule ^favicon\.ico$ /assets/images/favicon.ico [L]
    
    # 阻止直接访问admin/Plugin目录（但允许特定插件文件）
    RewriteCond %{REQUEST_URI} !^/admin/Plugin/(12_wechatlogin\.php|13_qqlogin\.php|36_cart\.php|40_stamp\.php|52_emaillink\.php|53_modpwd\.php|wx\.php)$ [NC]
    RewriteRule ^admin/Plugin/.*$ - [F,L]
    
    # 排除toolsrc目录下的HTML文件，允许直接访问
    RewriteCond %{REQUEST_URI} ^/toolsrc/.*\.html$ [NC]
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule ^ - [L]
    
    # toolsrc目录PHP文件统一验证
    RewriteCond %{REQUEST_URI} !^/toolsrc/verify\.php$ [NC]
    RewriteCond %{REQUEST_URI} ^/toolsrc/.*\.php$ [NC]
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule ^toolsrc/(.*\.php)$ /toolsrc/verify.php?file=$1 [L,QSA]
    
    # 排除aaa目录下的HTML文件，允许直接访问
    RewriteCond %{REQUEST_URI} ^/aaa/.*\.html$ [NC]
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule ^ - [L]
    
    # 排除.master目录下的文件，允许直接访问
    RewriteCond %{REQUEST_URI} ^/\.master/ [NC]
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule ^ - [END]
    
    # 将/install.php重写到templates/install.php
    RewriteRule ^install\.php$ /templates/install.php [L]
    
    # 直接提供payapi.php、recharge.php和payewm.php文件
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule ^templates/payapi\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule ^templates/recharge\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule ^templates/payewm\.php$ - [L]
    
    # 允许直接访问install.php文件
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule ^templates/install\.php$ - [L]
    
    # 对于其他HTML文件，强制通过PHP处理以支持标签解析
    RewriteCond %{REQUEST_URI} \.html$ [NC]
    RewriteRule ^(.*)$ index.php [L]
    
    # 排除admin目录，直接访问admin目录下的文件
    RewriteCond %{REQUEST_URI} ^/admin/ [NC]
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule ^ - [L]
    
    # 确保admin/ajaxx.php可以直接访问
    RewriteCond %{REQUEST_URI} ^/admin/ajaxx\.php$ [NC]
    RewriteRule ^ - [L]
    
    # 将/admin/重写到/admin/admin.php（当不是特定查询字符串时）
    RewriteCond %{REQUEST_URI} ^/admin/?$ [NC]
    RewriteCond %{QUERY_STRING} !^tab=articles(&action=(add|edit))?(.*)$ [NC]
    RewriteRule ^admin/?$ /admin/admin.php [L,QSA]
    
    # 后台文章编辑和发布URL重写规则 - 将带admin.php的URL重定向到干净URL
    RewriteCond %{REQUEST_URI} ^/admin/admin\.php$ [NC]
    RewriteCond %{QUERY_STRING} ^tab=articles(&action=(add|edit))?(.*)$ [NC]
    RewriteRule ^(admin/admin\.php)$ /admin/?%{QUERY_STRING} [R=302,L]
    
    # 后台文章编辑和发布URL重写规则 - 将干净URL重写到admin.php
    RewriteCond %{REQUEST_URI} ^/admin/$ [NC]
    RewriteCond %{QUERY_STRING} ^tab=articles(&action=(add|edit))?(.*)$ [NC]
    RewriteRule ^(admin/)$ $1admin.php [L,QSA]
    
    # 对于存在的文件，直接提供（但排除.qkbbs文件）
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteCond %{REQUEST_URI} !\.qkbbs$ [NC]
    RewriteCond %{REQUEST_URI} !\.html$ [NC]
    RewriteRule ^ - [L]
    
    # 重写所有非文件请求到index.php，包括/admin/这样的URL
    RewriteRule ^(.*)$ index.php [L]
</IfModule>
