templets/ 目錄下所有 .htm、.lbi 模板文件https://site.com/templets/default/index.htm 獲取模板源碼在 Web 服務器層面禁止對模板文件後綴(.htm、.lbi)的直接 HTTP 訪問,返回 403 Forbidden。DedeCMS 內部引入模板使用 include/require 文件系統路徑,不受 Web 訪問限制影響。
在網站根目錄創建/編輯 .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
# 禁止直接訪問模板文件
RewriteRule ^(.*)\.htm$ - [F,L]
RewriteRule ^(.*)\.lbi$ - [F,L]
</IfModule>
# 兼容無 Rewrite 模塊環境
<IfModule !mod_rewrite.c>
Order Deny,Allow
Deny from all
<FilesMatch "\.(htm|lbi)$">
Allow from all
</FilesMatch>
</IfModule>
關鍵指令:
F = Forbidden (403)L = Last (停止後續規則)[ISAPI_Rewrite]
RewriteRule ^(.*)\.htm$ - [F,L]
RewriteRule ^(.*)\.lbi$ - [F,L]
<configuration>
<system.webServer>
<security>
<requestFiltering>
<fileExtensions>
<add fileExtension=".htm" allowed="false" />
<add fileExtension=".lbi" allowed="false" />
</fileExtensions>
</requestFiltering>
</security>
<rewrite>
<rules>
<rule name="BlockTemplateFiles" stopProcessing="true">
<match url=".*\.(htm|lbi)$" />
<action type="CustomResponse" statusCode="403" statusReason="Forbidden" statusDescription="Template file access denied" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
# 在 server {} 或 location / {} 內添加
location ~* \.(htm|lbi)$ {
deny all;
return 403;
}
重載 Nginx:nginx -s reload
https://yoursite.com/templets/default/index.htm → 應返回 403.html/.htm),需調整規則僅針對 templets/ 目錄:# Nginx 精確匹配目錄
location ~* /templets/.*\.(htm|lbi)$ { deny all; }
# Apache
RewriteRule ^templets/(.*)\.(htm|lbi)$ - [F,L]
Web 服務器層面禁止模板文件直接訪問,是成本最低、效果最直接的防盜用手段。配合 Nginx/Apache/IIS 原生模塊,零代碼侵入、零性能損耗、升級無感知。建議作為站點上線標準配置項納入部署清單。