2012年6月7日星期四

Z-Blog分类标签全站静态化方案

  Z-Blog目前已经实现了首页和文章页的静态化,可以生成HTML文件,但是系统的分类页、标签Tags页、归档页和作者页都没有实现静态化,本文将讲述一个非常简单的方案,能够自动生成各个分类页、归档页和Tags的静态HTML页面,以便用户将网站迁移到Apache等不支持ASP的主机上。

  Z-Blog的分类页、标签Tags页、归档页和作者页都是调用catalog.asp这个文件,因此,修改这个文件,当用户调用该文件的时候,自动生成针对该页面的HTML文件。如果访客反复读取这个文件,那么在一定时间范围内,可以直接在文件开头使用静态文件,以达到节省系统资源的目的。

  具体的修改方法是,先在根下建立一个目录cat,然后使用编辑器打开Z-Blog根目录下的catalog.asp文件,在文件开头加入如下的代码:

Dim objFSO
Dim objFile
Dim strFileName
Dim strFileTime
Dim isBuildFile
if Request.QueryString("cate")<>"" then
 if Request.QueryString("page")<>""then
  strFileName = "cate" + "_" + Request.QueryString("cate") + "_" + Request.QueryString("page") +".html"
 else
  strFileName = "cate" + "_" + Request.QueryString("cate") + ".html"
 end if
elseif Request.QueryString("tags")<>"" then
 if Request.QueryString("page")<>""then
  strFileName = "tags" + "_" + Request.QueryString("tags") + "_" + Request.QueryString("page") +".html"
 else
  strFileName = "tags" + "_" + Request.QueryString("tags") + ".html"
 end if
elseif Request.QueryString("auth")<>"" then
 if Request.QueryString("page")<>""then
  strFileName = "auth" + "_" + Request.QueryString("auth") + "_" + Request.QueryString("page") +".html"
 else
  strFileName = "auth" + "_" + Request.QueryString("auth") + ".html"
 end if
elseif Request.QueryString("date")<>"" then
 if Request.QueryString("page")<>""then
  strFileName = "date" + "_" + Request.QueryString("date") + "_" + Request.QueryString("page") +".html"
 else
  strFileName = "date" + "_" + Request.QueryString("date") + ".html"
 end if
elseif Request.QueryString("page")<>"" then
 strFileName = "default" + "_" + Request.QueryString("page") +".html"
else
 strFileName = "default_1" + ".html"
end If
isBuildFile = False
Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FileExists(Server.MapPath(strFileName)) Then
 Set objFile = objFSO.GetFile(Server.MapPath(strFileName))
 strFileTime = objFile.DateLastModified
 Set objFile = Nothing
 If datediff("h",strFileTime,Now()) > 12 Then
  isBuildFile = True
 Else
  Server.Transfer strFileName
  Response.End
 End If
Else
 isBuildFile = True
End If
Set objFSO = Nothing

  找到 Response.Write ArtList.html 一行,在其后面增加如下代码:

If isBuildFile Then
    ArtList.FileName=strFileName
    ArtList.Directory="cat"
   ArtList.Save
End if

  这样,系统就会自动声称全部动态页面的静态HTML文件,并且在1小时内不会重复生成,期间如果还有调用,则自动载入静态HTML文件,以节省系统资源。

  经过这番处理,Z-Blog的全部分类和Tags就都可以生成静态HTML页面了。